Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 协会不';无法使用依赖项删除:true_Ruby On Rails 4_Associations_Destroy - Fatal编程技术网

Ruby on rails 4 协会不';无法使用依赖项删除:true

Ruby on rails 4 协会不';无法使用依赖项删除:true,ruby-on-rails-4,associations,destroy,Ruby On Rails 4,Associations,Destroy,我有一个用于项目和照片模型的加入模型 class ItemPhoto < ActiveRecord::Base belongs_to :item belongs_to :photo end class Item < ActiveRecord::Base has_many :item_photos has_many :photos, through: :item_photos, dependent: :destroy end class Photo

我有一个用于
项目
照片
模型的加入模型

class ItemPhoto < ActiveRecord::Base
    belongs_to :item
    belongs_to :photo
end

class Item < ActiveRecord::Base
    has_many :item_photos
    has_many :photos, through: :item_photos, dependent: :destroy
end


class Photo < ActiveRecord::Base
    has_many :item_photos, dependent: :destroy
    has_many :items, through: :item_photos, dependent: :destroy
end
class ItemPhoto
当删除一张照片时,如果它是最后一张照片(某种参考计数),我想删除该项目,或者至少能够在之前运行一些代码


这可能吗?

实现这一点的一种方法是使用关联回调,如删除后的

class Photo < ActiveRecord::Base
  has_many :item_photos, dependent: :destroy
  has_many :items, through: :item_photos, dependent: :destroy, after_remove: :do_stuff

  private

  def do_stuff
    # ...
  end
end
class-Photo
此处的文档:(无直接链接,但搜索关联回调)