【问题标题】:How can I make a polymorphic connection be dependent?如何使多态连接依赖?
【发布时间】:2011-03-24 05:10:08
【问题描述】:

我试图让我的 image_maps 在删除产品或图像时被销毁。这是代码。

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

现在,当您删除图像时,image_maps 不会被破坏,我仍然需要弄清楚如何让它也适用于产品。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 polymorphic-associations polymorphism


    【解决方案1】:

    我认为你的依赖应该去 ImageMap 模型。

    如果我没记错的话,这是 Rails 检查相关模型以查看它们是否需要销毁的内容。

    【讨论】:

    • 我试过了,但是当我尝试将图像与产品相关联时,它删除了产品哈哈。
    • 嗯,由于关联,我想您需要回调。也许这有帮助:stackoverflow.com/questions/4998239/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2017-11-15
    • 1970-01-01
    相关资源
    最近更新 更多