Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecordExtensions.belongs_to with extra arguments is not working #235

Open
qsona opened this issue Aug 12, 2021 · 3 comments
Open

ActiveRecordExtensions.belongs_to with extra arguments is not working #235

qsona opened this issue Aug 12, 2021 · 3 comments

Comments

@qsona
Copy link

qsona commented Aug 12, 2021

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_hash to Gemfile, and bundle install
  • bundle exec rake db:create
  • bundle 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 c and add records, then Bar#foo can 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 load Bar class
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)
@kbrock
Copy link
Collaborator

kbrock commented Aug 12, 2021

I wonder if this is handled by #158
It is an old PR and stale by now - but is this addressing the same need?

@issei-m
Copy link

issei-m commented Sep 29, 2021

Any update?

@aert
Copy link

aert commented Oct 4, 2023

Having same issue here, rails 7.0.8, active_hash 2.3.0...

@qsona The temporary fix works, thanks!

@kbrock the needs seems not to be the same, basically I just added class_name: '...' to the belongs_to call and it is sufficient to raise the exception described.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants