Reputation: 28
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.
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.
Upvotes: 0
Views: 636
Reputation: 2575
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
.
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.
Upvotes: 1