【发布时间】:2013-03-12 11:51:38
【问题描述】:
我在使用 Rails 4.0.0.beta 时收到以下错误:
NoMethodError: undefined method `primary_key_name' for #<ActiveRecord::Reflection::AssociationReflection
我在使用 Rails 3.2.x 时没有遇到异常。
我将 Ruby 1.9.3-p194 用于 Rails 3.2.13 和 Rails 4.0.0.beta。
问题源于以下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,但这些不应导致 undefined method 异常。
【问题讨论】:
-
什么时候出现这个错误?运行测试?我在 Rails 4 存储库中看到“primary_key_name”引用的唯一地方是ActiveRecord::FixtureSet。否则,可能是与 Rails 4 不兼容的 gem。
-
我在尝试使用 Seedbank gem 填充种子时遇到此错误。但是,我尝试只执行一些涉及商店模型的创建语句(控制台),并且在该行上也失败了。我认为这与种子库或一般播种无关。这肯定与
has_many :customers有关。错误的行号也将此行标识为罪魁祸首。我查找了该方法,也只能找到 ActiveRecord::FixtureSet 的引用。自 3.1.0 起,ActiveRecord::Reflection::AssociationReflection. primary_key_name方法已被弃用。 -
我正在一一消除项目的gem依赖...我希望找到其中一个是罪魁祸首。
-
您可以在 gems 目录中搜索“primary_key_name”,看看是否有任何明确引用它。
-
@Beerlington,如果您想将您的建议作为答案发布,我很高兴将您接受的答案授予您。
标签: ruby-on-rails rails-activerecord ruby-on-rails-4