How to model a polymorphic relationship?

35 views
Skip to first unread message

Reiner Pittinger

unread,
Sep 9, 2008, 5:15:00 PM9/9/08
to rubyonra...@googlegroups.com
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,


Reiner
--
Posted via http://www.ruby-forum.com/.

Ilan Berci

unread,
Sep 9, 2008, 5:25:22 PM9/9/08
to rubyonra...@googlegroups.com
Reiner,

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?? :)

hth
ilan

Reiner Pittinger

unread,
Sep 9, 2008, 5:43:51 PM9/9/08
to rubyonra...@googlegroups.com
Hi Ilan,

thank you for your answer:

Ilan Berci wrote:

> 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
>

Well, this is exacly what I want to avoid. As the project grows, more
and more classes can be linked to each other. With your approach, each
class would start with a listing of "hard-wired" associations.

Imagine 10 classes being able to be linked to each other - what a mess
to maintain in each class!


Instead, I would like something like this:

class Relationship < ActiveRecord::Base
has_one :the_one # can be any model
has_one :the_other # can be any model
end

or, even more flexible

class Relationship < ActiveRecord::Base
has_many :members # a list model of any type
end

But how to do this with "the magic" of Rails?

Ilan Berci

unread,
Sep 10, 2008, 9:01:57 AM9/10/08
to rubyonra...@googlegroups.com
Reiner,

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

unread,
Sep 10, 2008, 2:38:54 PM9/10/08
to rubyonra...@googlegroups.com
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 ;) 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?

Ar Chron

unread,
Sep 10, 2008, 3:51:17 PM9/10/08
to rubyonra...@googlegroups.com
Search for "Rails double polymorphic" in Google...

http://wiki.rubyonrails.org/rails/pages/ManytoManyPolymorphicAssociations
http://blog.evanweaver.com/files/doc/fauna/has_many_polymorphs/files/README.html

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

Reiner Pittinger

unread,
Sep 18, 2008, 5:06:45 PM9/18/08
to rubyonra...@googlegroups.com
I came to the solution using the has_many_polymorphs plugins:

The correct modelling is:

class Relationship < ActiveRecord::Base

belongs_to :relationship_owner, :polymorphic => true
belongs_to :relationship_partner, :polymorphic => true

acts_as_double_polymorphic_join(
:relationship_owners =>[:people, :realties, :documents],
:relationship_partners => [:people, :realties, :documents]
)

end

... and the migration:

class CreateRelationships < ActiveRecord::Migration
def self.up
create_table :relationships do |t|
t.string :name

t.references :relationship_owner, :polymorphic => true
t.references :relationship_partner, :polymorphic => true

t.timestamps
end
end

def self.down
drop_table :relationships
end
end

Reply all
Reply to author
Forward
0 new messages