Rails Polymorphic Association与多个模型

时间:2021-04-21 20:15:27

This is the case: I have 4 models which are "owner", "user", "location" and "landlord". All of these models share email addresses and phones. So I'm thinking to use Polymorphic Association and I made a research but I just see cases for 3 models. In my case as you can see I will have more than 3 models.

情况就是这样:我有4个模型,分别是“所有者”,“用户”,“位置”和“房东”。所有这些型号共享电子邮件地址和电话。所以我想使用Polymorphic Association,我做了一个研究,但我只看到3个模型的案例。在我的情况下,你可以看到我将有超过3个型号。

So, do you think is a good idea to implement this kind of logic where I want to use a model like the "repository" for all emails and phones numbers?

那么,你认为实现这种逻辑是一个好主意,我想对所有电子邮件和电话号码使用像“存储库”这样的模型吗?

There is a limit or something in order to use that kind of association?. I'm thinking in some models like:

为了使用这种关联,有一个限制或某种东西?我在想一些模型,比如:

email
emailable
user
owner
landlord
location

Each model will have their necessary fields.

每个模型都有其必要的字段。

Thanks in advance.

提前致谢。

1 个解决方案

#1


1  

There's no limit. A polymorphic association is an open interface that any other model can plug into. In your example, maybe you have a Contact model, which belongs_to :contactable, polymorphic: true. The contacts table will need two indexed columns: contactable_id:integer, and contactable_type:string. Any other model can be contactable, as long as it has_one :contact, as: :contactable.

没有限制。多态关联是一个开放的接口,任何其他模型都可以插入。在您的示例中,也许您有一个Contact模型,belongs_to:contactable,polymorphic:true。 contacts表将需要两个索引列:contactable_id:integer和contactable_type:string。任何其他模型都可以联系,只要它has_one:contact,as :: contactable。

As far as if it's a good idea, I'd say if you think you will need to work with contacts as a separate entity from the contactable models, then this is a good solution. However, if you won't need to deal directly with contacts then it might be overcomplicating when you could just add email and phone fields to these models.

至于这是一个好主意,我会说如果你认为你需要将联系人作为一个单独的实体与可联系的模型一起工作,那么这是一个很好的解决方案。但是,如果您不需要直接与联系人打交道,那么当您可以向这些模型添加电子邮件和电话字段时,它可能会过于复杂。

#1


1  

There's no limit. A polymorphic association is an open interface that any other model can plug into. In your example, maybe you have a Contact model, which belongs_to :contactable, polymorphic: true. The contacts table will need two indexed columns: contactable_id:integer, and contactable_type:string. Any other model can be contactable, as long as it has_one :contact, as: :contactable.

没有限制。多态关联是一个开放的接口,任何其他模型都可以插入。在您的示例中,也许您有一个Contact模型,belongs_to:contactable,polymorphic:true。 contacts表将需要两个索引列:contactable_id:integer和contactable_type:string。任何其他模型都可以联系,只要它has_one:contact,as :: contactable。

As far as if it's a good idea, I'd say if you think you will need to work with contacts as a separate entity from the contactable models, then this is a good solution. However, if you won't need to deal directly with contacts then it might be overcomplicating when you could just add email and phone fields to these models.

至于这是一个好主意,我会说如果你认为你需要将联系人作为一个单独的实体与可联系的模型一起工作,那么这是一个很好的解决方案。但是,如果您不需要直接与联系人打交道,那么当您可以向这些模型添加电子邮件和电话字段时,它可能会过于复杂。