Rails 模型设计:将一组属性从一个类复制到另一个类

发布于 2024-12-10 15:22:29 字数 156 浏览 0 评论 0原文

我有一个 Invite 模型,它有 n + m 个属性(位置、时间等)。当接受邀请时,将创建一个 Event 对象,其中包含从 Invite 模型复制的 n 个属性和它自己的 k 个属性。我想避免 n 个属性重复。

我应该如何在 Rails 中建模这种关系?

谢谢。

I have a Invite model, which has n + m attributes (location, timing etc.). When an invite is accepted, an Event object is created with n attributes copied from Invite model and k attributes of its own. I want to avoid duplication of n attributes.

How should I model this relationship in Rails?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我们的影子 2024-12-17 15:22:29

不要考虑属性,而要考虑对象以及它们在现实世界中建模(代表)的内容。还要考虑对象的生命周期

  • 邀请是很容易理解的
  • 事情 事件也是可以理解的

仅仅因为存在重叠并不意味着您想要消除它。

如果您确实愿意,您可以说每个活动都属于一个邀请。 (n 属性仅存储在所属的 Invite 模型中。)

但在执行此操作之前,请使用业务逻辑确保邀请一旦达到 accepted 状态就不会更改。

添加 复制 n 属性的另一个原因:假设这些属性稍后针对实际事件进行了更改 - 您希望这些属性位于事件模型中。如果您有邀请和事件的一组数据,那么您会说这些属性从输入邀请时起就固定了。

但这很可能与现实世界不符。如果您确实更改了这些值,那么您将丢失输入邀请时原始值的信息。

Don't think about the attributes, think about the objects and what they model (represent) in the real world. Also think about the life-cycle of the objects

  • Invites are something easily understandable
  • Events are also understandable

Just because there is an overlap doesn't mean that you want to eliminate it.

If you really want, you could say that every Event belongs_to an Invite. (And the n attributes are stored only in the owning Invite model.)

But before you do this, ensure with business logic that Invites are never changed once they reach the accepted state.

Added Another reason to duplicate the n attributes: Suppose those attributes are later changed for the actual event--you'd want the attributes to be in the Event model. If you had one set of data for the Invite and the Event, then you'd be saying that those attributes are fixed from the time the Invite was entered.

But that may well not match with the real world. And if you do change the values, then you'd lose the information of what the values originally were when the invite was entered.

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