Rails:String的未定义方法`primary_key':Class

Jug*_*ang 5 activerecord ruby-on-rails

我有3个类,Schema,Entity和Property,代表数据库设计业务.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
Run Code Online (Sandbox Code Playgroud)

当我运行entity_obj.properties时,它会抛出错误undefined method primary_key' for String:Class.

我扭曲了has_many的选项,但它没有帮助.

有没有人对此有任何想法?

谢谢.

Jug*_*ang 5

谢谢muttonlamb!

我解决了这个问题.

一开始,我想问题has_many就出现了,因为那是它出现的地方.但事实并非如此.即使我没有定义class_name,Rails仍然可以找到这个类.

后来我发现一些记录显示属性typein Property没有赋值.根本原因是我覆盖了超类的属性!

解决方案:

ActiveRecord::Migration.rename_column :hx_properties , :type, :datatype
Run Code Online (Sandbox Code Playgroud)


mut*_*amb 3

我认为你需要去掉“”HX::Property

错误消息告诉您它正在尝试调用 String:Class 对象上的主键