active-hash / active-hash/active_hash
ActiveRecordExtensions.belongs_to with extra arguments is not working
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 1.4k
- Forks
- 186
- PR merge metrics
- No merged PRs in 30d
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
end
But this doesn't work.
class Bar < ApplicationRecord
extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to :foo, primary_key: :code, foreign_key: :foo_code
end
error 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
end
This 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)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by inspecting ActiveHash::Associations::ActiveRecordExtensions and reproduce the failure using the app/models/bar.rb example with ActiveRecord 6.1.4. Compare the extension's belongs_to handling with the documented ActiveRecord association arguments; done means extra arguments such as primary_key and foreign_key work when the extension is applied, without breaking belongs_to_active_hash.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rails, ruby
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100