早安各位溢出者,
模型关联的小问题。我有这些模型关联:
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 有:exhibit_id=>'1'
)当我给出rails console
:Categorization.find_by_exhibit_id(1).destroy
谢谢你的帮助!!