Rails 4:未定义的方法`primary_key_name'

Dom*_*Dom 4 ruby-on-rails ruby-on-rails-4 rails-activerecord

我使用Rails 4.0.0.beta收到以下错误:

NoMethodError: undefined method `primary_key_name' for #<ActiveRecord::Reflection::AssociationReflection
Run Code Online (Sandbox Code Playgroud)

使用Rails 3.2.x时,我没有得到异常.

我在Rails 3.2.13和Rails 4.0.0.beta上使用Ruby 1.9.3-p194.

问题源于以下has_many声明:

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
Run Code Online (Sandbox Code Playgroud)

我有以下支持课程:

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

class Relationship < ActiveRecord::Base
  belongs_to :store
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

我想知道如何has_many使用Rails 4友好代码实现相同的功能?

PS我知道我仍然使用旧式语法,而条件哈希现在需要一个proc或lambda,但这些不应该导致undefined method异常.

Dom*_*Dom 7

我找到了罪魁祸首,它是perfectline/validates_existence默认为弃用的宝石而primary_key_name不是- 去的foreign_key时候ActiveRecord::VERSION::MINOR >= 1!我已经提交了一个带有修复程序的拉取请求(https://github.com/perfectline/validates_existence/pull/20).谢谢你指出我正确的方向@Beerlington.