こんばんは、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_id
、exhibit_id
およびcategory_id
.
私の問題は、Exhibit を削除すると、Categorization テーブルの参照が削除されないため、ビューで DB エラーが発生することです。最初に展示品をカテゴリから割り当て解除してから、安全に削除する必要があります。または(たとえば、私が削除した展示物に があると仮定すると:exhibit_id=>'1'
)rails console
:Categorization.find_by_exhibit_id(1).destroy
助けてくれてありがとう!!