微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – Rails 4:未定义的方法`primary_key_name’

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

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

使用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

我有以下支持课程:

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

class Relationship < ActiveRecord::Base
  belongs_to :store
  belongs_to :user
end

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

附:我知道我仍然使用旧式语法,条件哈希现在需要proc或lambda,但这些不应该导致未定义的方法异常.

解决方法

我找到了罪魁祸首,它是perfectline / validates_existence gem,当ActiveRecord :: VERSION :: MInor> = 1时,认为不推荐使用的primary_key_name而不是foreign_key!我已经使用修复程序( https://github.com/perfectline/validates_existence/pull/20)提交了一个pull请求.谢谢你指着我正确的方向@Beerlington.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐