加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 02:45:02 所属栏目:百科 来源:网络整理
导读:我使用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. 问题源
我使用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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读