Skip to content

ActiveRecordExtensions.belongs_to with extra arguments is not working #235

@qsona

Description

@qsona

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions