ruby-on-rails – ror – 在has_many和belongs_to的两端包含外键
发布时间:2020-12-17 03:02:03 所属栏目:百科 来源:网络整理
导读:我继承的代码有: class Graphic ActiveRecord::Base has_many :comments,:foreign_key = 'asset_id',:conditions = 'asset_type_id = 5',:order = 'created_at',:dependent = :destroyclass Comment ActiveRecord::Base belongs_to :graphic,:foreign_key =
我继承的代码有:
class Graphic < ActiveRecord::Base has_many :comments,:foreign_key => 'asset_id',:conditions => 'asset_type_id = 5',:order => 'created_at',:dependent => :destroy class Comment < ActiveRecord::Base belongs_to :graphic,:foreign_key => :asset_id 在我看来,像has_many不应该有foreign_key(它在belongs_to中被引用,我相信)但我不确定,你知道吗? 也就是说 class Graphic < ActiveRecord::Base has_many :comments,:foreign_key => :asset_id 解决方法
我想你正在尝试做一些已经在Rails中烘焙过的东西.您应该在这里使用多态关联.
class Comment belongs_to :asset,:polymorphic => true end class Graphic has_many :comments,:as => :assets end 这样,你需要在任何一方都声明foreign_key. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容