Rails 4:未定义的方法`primary_key_name' [英] Rails 4: undefined method `primary_key_name'

查看:27
本文介绍了Rails 4:未定义的方法`primary_key_name'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm receiving the following error using Rails 4.0.0.beta:

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

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

I don't get the exception when using Rails 3.2.x.

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

I'm using Ruby 1.9.3-p194 for both Rails 3.2.13 and Rails 4.0.0.beta.

问题源于以下 has_many 语句:

The problem stems from 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

我有以下支持类:

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 功能?

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

附言我知道我仍在使用旧式语法,并且条件散列现在需要 proc 或 lambda,但这些不应导致 undefined method 异常.

P.S. I'm aware I'm still using old-style syntax, and that the conditions hash now requires a proc or lambda, but these shouldn't causing the undefined method exception.

推荐答案

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

I found the culprit, it's the perfectline/validates_existence gem which defaults to the deprecated primary_key_name instead of foreign_key when the ActiveRecord::VERSION::MINOR >= 1 - go figure! I've submitted a pull request with the fix (https://github.com/perfectline/validates_existence/pull/20). Thanks for pointing me in the right direction @Beerlington.

这篇关于Rails 4:未定义的方法`primary_key_name'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆