Rails 4: undefined method `primary_key_name '

I get the following error using Rails 4.0.0.beta:

NoMethodError: undefined method `primary_key_name' for #<ActiveRecord::Reflection::AssociationReflection 

I get no exception when using Rails 3.2.x.

I am using Ruby 1.9.3-p194 for Rails 3.2.13 and Rails 4.0.0.beta.

The problem is with the following has_many statement:

 class Store < ActiveRecord::Base has_many :relationships has_many :customers, :through => :relationships, :source => :user, :conditions => { :relationships => { :description => "Customer" } } do def <<(user) proxy_association.owner.relationships.create(:description => "Customer", :user => user) end end end 

I have the following supporting classes:

 class User < ActiveRecord::Base has_one :relationship has_one :store, :through => :relationship end class Relationship < ActiveRecord::Base belongs_to :store belongs_to :user end 

I would like to know how I can achieve the same has_many functionality using Rails 4-friendly code?

PS I know that I still use the old-style syntax and that the condition hash now requires proc or lambda, but they should not throw an undefined method exception.

+3
ruby-on-rails ruby-on-rails-4 rails-activerecord
Mar 12 '13 at 11:51
source share
1 answer

I found the culprit, this is the perfectline/validates_existence gem, which by default uses the deprecated primary_key_name instead of foreign_key when the symbol is ActiveRecord::VERSION::MINOR >= 1 - go! I sent a pull request with a fix ( https://github.com/perfectline/validates_existence/pull/20 ). Thanks for pointing me in the right direction @Beerlington.

+7
Mar 12 '13 at 15:01
source share



All Articles