1

早安各位溢出者,

模型关联的小问题。我有这些模型关联:

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_idexhibit_idcategory_id.

我的问题是,当我删除一个展览时,它在分类表上的引用没有被删除,因此在我的视图中出现了数据库错误。我必须首先从任何类别中取消分配展览,然后安全地删除它。或者(例如,我删除的 Exhibit 有:exhibit_id=>'1')当我给出rails consoleCategorization.find_by_exhibit_id(1).destroy

谢谢你的帮助!!

4

1 回答 1

2

您可以:dependent在删除父项时希望 Rails 遵循的关联上设置选项:

class Exhibit < ActiveRecord::Base
  has_many :categorizations, :dependent => :destroy
  ...
end
于 2011-07-30T11:20:57.670 回答