active-hash/active_hash

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

Closed this issue · 4 comments

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.

@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.

I have created a PR that may solve this issue.
#306

Closed by #306