问题描述
我想保留子记录和层次结构,即使删除父记录也是如此.我看到两个选项:
I want to keep child records and the hierarchy, even when the parent is deleted. I see two options:
- 保留现有父项并使用deleted_at"字段来指示parent 本身是不活动的,但关系仍然存在.这将导致永久存储许多有效的死父记录.嗯.
- 将所有废弃的子记录分配给一个通用的收集器"僵尸父记录.我更喜欢这个,但是这样你就会失去子记录原始来源的历史记录.
我没有 Rails 经验,无法预见这两条中哪一条是最可取的,或者可能有完全不同的解决方案.
I don't have the Rails experience to see ahead as to which of these 2 would be the most advisable path to take, or maybe there's an altogether different solution.
SO 告诉我这似乎是一个主观问题,他们可能会关闭它.我希望不会,因为我相信其他人也有这个问题.
SO is telling me this appears to be a subjective question and they may close it. I hope not, because I'm sure this is a question that others have as well.
推荐答案
在我看来,您基本上是在询问软删除"功能.当我想要这种行为时,我通常会添加一个默认为 true
的 active
属性.我还在模型中添加了一个 active
范围,这样我就可以执行诸如 Salon.active
之类的操作来方便地获取所有活动的内容.
It seems to me like you're basically asking about "soft delete" functionality. When I want this kind of behavior, I usually add an active
attribute that defaults to true
. I also add an active
scope to the model so I can do something like Salon.active
to conveniently get everything that's active.
所以我想我的答案是我会做一些类似 #1 的事情,我称之为软删除.想法 #2 对我来说似乎很疯狂.
So I guess my answer is that I'd do something like #1, which I would call a soft delete. Idea #2 seems pretty crazy to me.
这篇关于Rails 4+ 最佳实践:删除父级同时保留子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!