Rails 多态依赖::destroy 无法正常工作 [英] Rails polymorphic dependent: :destroy not working correctly

查看:45
本文介绍了Rails 多态依赖::destroy 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有三个 Active Record 模型:

class 组织has_many :boogers, as: :boogerable, 依赖: :destroy结尾手指类has_many :boogers, as: :boogerable, 依赖: :destroy结尾类鼻屎own_to :boogerable, 多态: true结尾

假设我的应用程序可以将鼻屎从手指转移到纸巾上,一旦完成,手指就会被破坏(哎呀!).出于某种原因,当手指被破坏时,它也会破坏以前属于它并随后转移到组织上的鼻屎.在日志中我可以看到,当手指被破坏时,它会记住曾经属于它的鼻屎的 ID 并销毁它们.它不会检查以确保 boogerable_type 仍然是 'finger',从而破坏了类型为 'tissue' 的那些.

当手指被破坏时,它是这样做的:

DELETE FROM boogers WHERE booger.id = 387

什么时候应该这样做:

DELETE FROM boogers WHERE boogerable_id = 1 AND boogerable_type = 'finger'

有人遇到过这种情况吗?

解决方案

首先需要知道 ActiveRecord destroy 方法使用对象的 :id 字段时需要摧毁它.并且 dependent: :destroy 选项将在与当前对象关联的所有对象上触发 destroy 方法.

我假设你正在做这样的事情:

f = Finger.find(finger_id)t = Tissue.find(tissue_id)f.boogers[0].boogerable = tf.boogers[0].save!f.销毁

dependent: :destroy 声明意味着当 destroyf 上被调用时,它也会在所有关联的鼻屎上被调用用 f.并且由于 f 和它的鼻屎被加载到内存中,f.booger[0] 对象仍然存在于 f.booger 的数组中当 f 被销毁时,它的每个元素,包括 boogers[0] 都会被销毁.

这种情况的解决方案是您在调用 f.destroy 之前点击 f.boogers.reload 来更新数组.

另外,请注意,当你销毁f时,所有关联的boogers也将被destroy,这真的是你想要的吗?当其中一个被转移到其他东西时,销毁带有所有剩余关联boogers 的手指?

Let's say I have three Active Record models:

class Tissue
  has_many :boogers, as: :boogerable, dependent: :destroy
end

class Finger
  has_many :boogers, as: :boogerable, dependent: :destroy
end

Class Boogers
  belongs_to :boogerable, polymorphic: true
end

Let's say my application can transfer a booger from a finger to a tissue, and once that's done, the finger is destroyed (yikes!). For some reason, when the finger is destroyed, it is also destroying the boogers that previously belonged to it and were since transferred to a tissue. In the log I can see that when the finger is destroyed it remembers the ID of boogers that USED to belong to it and destroys them. It doesn't check to make sure the boogerable_type is still 'finger' and thus destroys the ones with type 'tissue'.

When a finger is destroyed, it is doing this:

DELETE FROM boogers WHERE booger.id = 387

When it should be doing this:

DELETE FROM boogers WHERE boogerable_id = 1 AND boogerable_type = 'finger'

Anyone come across this before?

解决方案

First, you need to know that the ActiveRecord destroy method uses the object's :id field when it needs to destroy it. And that the dependent: :destroy option will trigger the destroy method upon all objects that are assciated with the current object.

I assume that you're doing something like this:

f = Finger.find(finger_id)
t = Tissue.find(tissue_id)

f.boogers[0].boogerable = t
f.boogers[0].save!

f.destroy

The dependent: :destroy statement means that when destroy is called upon f, it will also be called upon all boogers that are associated with f. And since f and its boogers are loaded into memory, the f.booger[0] object still exists in the array of f.boogers which will have each of its elements, including boogers[0], destroyed when f is destroyed.

The solution of this case is that you hit f.boogers.reload to update the array before calling f.destroy.

Also, please note that when you destroy f, all associated boogers will be destroyed as well, is that really what you want? destroy the finger with all remaining associated boogers when one of them is transferred to something else?

这篇关于Rails 多态依赖::destroy 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆