How to model a polymorphic relationship?

I have read many articles about polymorphic associations in Rails and also about plugins for this purpose (mainly the has_many_polymorphs plugin).

Unfortunately, I am still unsure how to solve my problem:

Let's say you have these models you want to set in relation to each other: Person, Document, Realty.

You want to link polymorphic objects with each other. Let's call the "join-class" Relationship. A Relationship has a name and at least two link-partners.

A Relationship between Person and Document could be called "Creator".

A Relationship between a Person and a Realty would have the name "Owner".

A Relationship between a Document and a Realty would have the name "Lease".

So, how to implement this idea, and to be able to easly extend the list of linkable objects? Is it possible to avoid any association-definitions in the linkable models?

Thank you for your help,



I don't believe you have the need for polymorphic associations in the above example as you have a seperate association "type" already specced out for each relation

class Person < ActiveRecord::Base has_many :documents has_many :realties end

class Document < ActiveRecord::Base belongs_to :owner, class_name => "Person" belongs_to :realty end

class Realty < ActiveRecord::Base belongs_to :owner, class_name => "Person" has_many :leases, class_name => "Document" end

Where do you need polymorphic associations in the above example?

p.s. I know next to nothing about real estate so please forgive me if I got your relation wrong between documents and realties.. does a realty have many leases or is it the other way around?? :slight_smile:

hth ilan


My suggestion then is to come up with your classes first and then make a design. A very common pitfall is to design for something that "may" happen and then later face over engineered code.. In the late nineties, we called this the YAGNI principle which stood for "You Aint Going to Need It!"

If you propose your new more complete class model, I can help with your polymorphic associations (If you still need them)..

Lastly, remember that PAs are not free, they add extra conditions to the select clauses which can be quite costly with improper or imcomplete indexing.

Reiner Pittinger wrote:

Dear Ilan,

although I just supplied just three basic models, my application already contains more than 15 different models that should be linked to each other.

Some more examples:

- I need a relationship between two documents ("Like: Document B is an attachment to Document A). - I need a relationship beween a Key-Class (the ones you use to open a house :wink: and a RealtyItem (name: "Key House-Entrance"). - I need a relationship between two Persons, to model something like "Assistent"

I could supply a dozen more. This is why already now I want a flexible modelling of these relationships.

I image a table to store the relationships like this:

id name the_one_id the_one_class the_other_id the_other_class 1 Owner 2 Person 3 RealtyItem 2 Assistant 5 Person 5 Person 3 Lease#333 5 Document 3 RealtyItem

It seems so simple - but how do I achieve it correctly using Rails?

Search for "Rails double polymorphic" in Google...

or just assume that everyone else will do the googling for you...