Rails - 删除一对多关系的一侧
简单问题:
如果我有一对多关系,例如:
class SalesPerson < ActiveRecord::Base
has_many :deals
end
class Deal < ActiveRecord::Base
belongs_to :sales_person
end
如何删除销售人员,而不会对与其相关的交易产生负面影响?这样做的用例是有人离开组织。我们仍然需要数据库中的交易记录,但不再需要销售人员记录。
在销售人员身上设置一个活跃/不活跃的标志会更好吗?
谢谢。
Quick question:
If I have a one-to-many relationship, such as this:
class SalesPerson < ActiveRecord::Base
has_many :deals
end
class Deal < ActiveRecord::Base
belongs_to :sales_person
end
how can I delete a Sales Person, without negatively impacting the deals associated with them? The use-case for this would be if someone left the organization. We still need a record of the deals in the database, but that sales person record is no longer needed.
Would it just be better to have an active/inactive flag on the sales person instead?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于这种情况,我使用 acts_as_paranoid,基本上它添加了一个新的时间戳列:
deleted_at
和覆盖您的一些 AR 查找器。For this kind of situations I've use acts_as_paranoid, basically It adds a new timestamps column:
deleted_at
and overrides some of your AR finders.