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

ruby-on-rails – Rails:String的未定义方法`primary_key’:Cl

发布时间:2020-12-17 03:36:48 所属栏目:百科 来源:网络整理
导读:我有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:
我有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

当我运行entity_obj.properties时,它会为String:Class抛出错误未定义方法primary_key’.

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

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

谢谢.

解决方法

谢谢muttonlamb!

我解决了这个问题.

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

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

解决方案:

ActiveRecord::Migration.rename_column :hx_properties,:type,:datatype

(编辑:李大同)

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

    推荐文章
      热点阅读