Ruby on rails 如何使多态连接成为依赖关系?
当产品或图像被删除时,我试图使我的图像映射被销毁。这是代码Ruby on rails 如何使多态连接成为依赖关系?,ruby-on-rails,ruby-on-rails-3,polymorphic-associations,polymorphism,Ruby On Rails,Ruby On Rails 3,Polymorphic Associations,Polymorphism,当产品或图像被删除时,我试图使我的图像映射被销毁。这是代码 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, :thr
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
class ImageMaptrue
属于:图像
结束
类产品:imageable
有很多:图像,:至=>:图像
结束
类映像:imageable,:dependent=>:destroy
结束
现在,当您删除图像时,图像映射不会被破坏,我仍然需要弄清楚如何使其也适用于产品。我认为您的家属应该使用图像映射模型
如果我没记错的话,这就是Rails检查关联模型以确定它们是否需要销毁的内容。我尝试过,但当我尝试将图像与产品关联时,它删除了产品lol.hmm,因为关联,我想您需要回调。也许这有助于: