Rails - 删除一对多关系的一侧

发布于 2024-09-16 21:05:27 字数 315 浏览 1 评论 0原文

简单问题:

如果我有一对多关系,例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攀登最高峰 2024-09-23 21:05:27

对于这种情况,我使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文