1

こんばんは、Overflower の皆さん、

モデルの関連付けに関する小さな問題。私はこれらのモデルの関連付けを持っています:

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_idおよびcategory_id.

私の問題は、Exhibit を削除すると、Categorization テーブルの参照が削除されないため、ビューで DB エラーが発生することです。最初に展示品をカテゴリから割り当て解除してから、安全に削除する必要があります。または(たとえば、私が削除した展示物に があると仮定すると: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 に答える