How can I make a polymorphic connection be dependent?
I am trying to make my image_maps get destroyed when either a product 开发者_如何学编程or image is deleted. Here is the code.
class ImageMap < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
belongs_to :image
end
class Product < ActiveRecord::Base
has_many :image_maps, :as => :imageable
has_many :images, :through => :image_maps
end
class Image < ActiveRecord::Base
has_many :image_maps, :as => :imageable, :dependent => :destroy
end
Right now the image_maps do not get destroyed when you delete an image and i still need to figure out how to get it to work for products too.
I think that your dependent should go to the ImageMap model.
If i remember correctly, this is what Rails checks on the associated models to see whether they need to be destroyed.
精彩评论