active-hash / active_hash

A readonly ActiveRecord-esque base class that lets you use a hash, a Yaml file or a custom file as the datasource
MIT License
1.19k stars 178 forks source link

"`has_many': wrong number of arguments (given 2, expected 1) (ArgumentError)" error is raised since 3.3.0 #305

Closed issei-m closed 3 months ago

issei-m commented 3 months ago

This PR introduces a new extension method has_many that overrides the one from ActiveRecord::Base, but they have a different argument signature, so the following code raises an error:

class Country < ActiveHash::Base
end

class Post < ActiveRecord::Base
  belongs_to :person
end

class Person < ActiveRecord::Base
  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to_active_hash :country

  # This association is for two ActiveRecord models
  has_many :posts, -> { order(created_at: :desc) }, dependent: :destroy
end

I'm passing three arguments to this has_many according to the original ActiveRecord's method signature:

def has_many(name, scope = nil, **options, &extension)

https://github.com/rails/rails/blob/v7.1.3.2/activerecord/lib/active_record/associations.rb#L1522

but extended one accepts only two:

def has_many(association_id, **options)

https://github.com/active-hash/active_hash/blob/v3.3.0/lib/associations/associations.rb#L9

Any idea to resolve it? I'm using Rails 7.1.3.

kbrock commented 3 months ago

@flavorjones ugh. we may need to check active record version when calling super

It sure gets tricky to support multiple versions of active record since the interfaces change over time.


To fix this, we'll probably need to conditionally define (or call) methods based upon the active record version.

sontixyou commented 3 months ago

I have created a PR that may solve this issue. https://github.com/active-hash/active_hash/pull/306

flavorjones commented 3 months ago

Closed by #306

flavorjones commented 3 months ago

Fixed in https://github.com/active-hash/active_hash/releases/tag/v3.3.1