-
Notifications
You must be signed in to change notification settings - Fork 190
Open
Description
Environment
- Ruby 3.0.2
- Rails (ActiveRecord) 6.1.4
- Active Hash 3.1.0
Problem
This works
class Bar < ApplicationRecord
belongs_to :foo, primary_key: :code, foreign_key: :foo_code
endBut this doesn't work.
class Bar < ApplicationRecord
extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to :foo, primary_key: :code, foreign_key: :foo_code
enderror message:
irb(main):004:0> Bar
/Users/qsona/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/activerecord-6.1.4/lib/active_record/associations/builder/association.rb:53:in `build_scope': undefined method `arity' for {:primary_key=>:code, :foreign_key=>:foo_code}:Hash (NoMethodError)
Note that this error occurs when the Bar class is first loaded, not when executing bar.foo .
Temporal solution
It can be avoided by first calling belongs_to and then extend ActiveHash::Associations::ActiveRecordExtensions.
e.g.
class Bar < ApplicationRecord
belongs_to :foo, primary_key: :code, foreign_key: :foo_code
extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to_active_hash :baz
endThis solution cannot be used when you extend ApplicationRecord or ActiveRecord::Base directly ( ActiveRecord::Base.extend ActiveHash::Associations::ActiveRecordExtensions (docs).
Steps to Reproduce the Problem
- execute
rails new my_active_hash_test --minimal - Add
active_hashto Gemfile, and bundle install bundle exec rake db:createbundle exec rails g model Foo,bundle exec rails g model Bar- Rewrite migration files
# db/migrate/20210812100709_create_foos.rb
class CreateFoos < ActiveRecord::Migration[6.1]
def change
create_table :foos, id: false do |t|
t.string :code, null: false, primary_key: true
t.timestamps
end
end
end
# db/migrate/20210812100830_create_bars.rb
class CreateBars < ActiveRecord::Migration[6.1]
def change
create_table :bars do |t|
t.string :foo_code, null: false
t.timestamps
end
end
end- Rewrite app/models/bar.rb
class Bar < ApplicationRecord
# extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to :foo, primary_key: :code, foreign_key: :foo_code
end- Execute
bundle exec rails cand add records, thenBar#foocan be called successfully
Foo.create!(code: 'foo1')
bar = Bar.create!(foo_code: 'foo1')
bar.foo #=> #<Foo:0x00007f86840a1510 code: "foo1", created_at: Thu, 12 Aug 2021 10:11:01.476738000 UTC +00:00, updated_at: Thu, 12 Aug 2021 10:11:01.476738000 UTC +00:00>- Rewrite app/models/bar.rb
class Bar < ApplicationRecord
extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to :foo, primary_key: :code, foreign_key: :foo_code
end- Execute
bundle exec rails c, and loadBarclass
Bar
/Users/qsona/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/activerecord-6.1.4/lib/active_record/associations/builder/association.rb:53:in `build_scope': undefined method `arity' for {:primary_key=>:code, :foreign_key=>:foo_code}:Hash (NoMethodError)
issei-m, bluesnotred, shikibum, Iwark and aertissei-m
Metadata
Metadata
Assignees
Labels
No labels