早上好,奥弗劳斯兄弟,
模型关联的小问题。我有这些模型关联:
class Categorization < ActiveRecord::Base
belongs_to :exhibit
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :categorizations
has_many :exhibits, :through => :categorizations
acts_as_indexed :fields => [:title]
validates :title, :presence => true, :uniqueness => true
end
class Exhibit < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations, :source => :category
acts_as_indexed :fields => [:title, :bulb]
validates :title, :presence => true, :uniqueness => true
belongs_to :foto, :class_name => 'Image'
end因此,本质上,Categorization以这些列(省略日期/时间戳)结束:categorization_id、exhibit_id和category_id。
我的问题是,当我删除一个Exhibit时,它在Categorization表上的引用并没有被删除,因此在我的视图上得到一个DB错误。我必须首先从任何类别中取消分配Exhibit,然后安全地删除它。或者(例如,假设我删除的Exhibit具有:exhibit_id=>'1'),当我在rails console:Categorization.find_by_exhibit_id(1).destroy中给出时
谢谢你的帮助!!
发布于 2011-07-30 19:20:58
您可以在希望Rails在删除其父关联时遵循的关联上设置:dependent选项:
class Exhibit < ActiveRecord::Base
has_many :categorizations, :dependent => :destroy
...
endhttps://stackoverflow.com/questions/6882759
复制相似问题