Rails 模型设计:将一组属性从一个类复制到另一个类
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要考虑属性,而要考虑对象以及它们在现实世界中建模(代表)的内容。还要考虑对象的生命周期
仅仅因为存在重叠并不意味着您想要消除它。
如果您确实愿意,您可以说每个活动都
属于
一个邀请。 (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
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 then
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.