0

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Joshua Novak
  • 144
  • 1
  • 10

1 Answers1

0

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.

Spyros
  • 46,820
  • 25
  • 86
  • 129
  • I tried it, but when i tried to associate an image with a product it deleted the product lol. – Joshua Novak Mar 24 '11 at 06:19
  • hmm, because of the associations, i suppose you need a callback. Maybe this helps : http://stackoverflow.com/questions/4998239/rails-help-understanding-how-to-use-dependent-destroy – Spyros Mar 24 '11 at 06:51