我有 3 个类,Schema、Entity 和 Property,代表 DB 设计业务。
Schema-Entity 似乎有效,但 Entity-Property 无效。
class Hx::Entity < ActiveRecord::Base
belongs_to :schema
attr_accessible :name
has_many :properties , class_name: "Hx::Property" , primary_key: "id"
end
class Hx::Property < ActiveRecord::Base
attr_accessible :destination, :indexed, :inverse, :isToMany, :kind, :name, :optional, :transient , :type
belongs_to :entity
end
当我运行 entity_obj.properties 时,它会抛出错误
undefined method primary_key' for String:Class
.我绕过了 has_many 的选项,但它没有帮助。
有没有人对此有任何想法?
谢谢。
最佳答案
谢谢羊肉串!
我解决了这个问题。
一开始,我猜问题是在has_many
附近因为那是它出现的地方。但事实并非如此。即使我没有定义 class_name
,Rails 还是可以找到类的。
后来我发现一些记录显示属性type
在 Property
没有赋值。根本原因是我覆盖了父类(super class)的属性!
解决方案:
ActiveRecord::Migration.rename_column :hx_properties , :type, :datatype
关于ruby-on-rails - rails : undefined method `primary_key' for String:Class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15517372/