# Multiple relationships between two models

**URL:** <https://www.ruby-forum.com/t/multiple-relationships-between-two-models/127704>\
**Category:** Rails\
**Created:** [January 31, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/multiple-relationships-between-two-models/127704 "2008-01-31T15:59:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [January 31, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/multiple-relationships-between-two-models/127704/1 "2008-01-31T15:59:53Z")

</div>

Hello all. I’m having trouble understanding how to set up multiple  
relationships between two models.

I have a database for an art gallery. It will be used to track people  
(artists, buyers, donors, press contacts, etc) and artworks. Thus, I  
have three main tables: “contacts,” “roles,” and “items,” plus a join  
table “contact\_roles.” A contact person can have multiple roles, i.e.  
an artist or press contact could also buy an artwork, or a donor could  
also be a volunteer.

Here are the tables and the classes:

create\_table :contacts do |t|  
t.column :first\_name, :string  
t.column :last\_name, :string  
end

create\_table :roles do |t|  
t.column :name, :string  
end

create\_table :contact\_roles do |t|  
t.column :contact\_id, :string  
t.column :role\_id, :string  
end

create\_table :items do |t|  
t.column :name, :string  
t.column :dimensions, :string  
t.column :medium, :string  
t.column :artist\_id, :string  
t.column :buyer\_id, :string  
end

class Contact \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :roles

def full\_name\_last  
last\_name + ", " + first\_name  
end  
end

class Artist \< Contact  
has\_many :items  
end

class Buyer \< Contact  
has\_many :items  
end

class Role \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :contacts  
end

class Item \< ActiveRecord::Base  
belongs\_to :artists  
belongs\_to :buyers  
end

My main question is: should I be using polymorphic associations to link  
the contacts and the items? I don’t quite understand polymorphic  
associations - any suggestions on web pages with good explanations for  
an object-oriented newbie?

If I don’t need to use polymorphism, then do I need to use parameters  
like :foreign\_key on the has\_many and belongs\_to due to the fact that  
artist/buyer are not semantically identical to “contacts”?

Thanks  
David

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [January 31, 2008, 4:29pm UTC](https://www.ruby-forum.com/t/multiple-relationships-between-two-models/127704/2 "2008-01-31T16:29:14Z")

</div>

[http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations](http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations)

I used it for Ratings, for example. Pretend you have a blog, which has  
Posts, Comments, and Pictures. You want all of them to have Rating,  
so use polymorphic.

I don’t know how this would apply here, though.

-Ryan

On Jan 31, 8:59 am, David C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [January 31, 2008, 4:41pm UTC](https://www.ruby-forum.com/t/multiple-relationships-between-two-models/127704/3 "2008-01-31T16:41:45Z")

</div>

Hello Ryan, thanks for your reply. That was a helpful link, but I  
don’t understand what happens in their example if an Order and a User  
have the same address. Do you wind up with two records in the  
addresses table, both the same except for addressable\_id and  
addressable\_type?

David

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [January 31, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/multiple-relationships-between-two-models/127704/4 "2008-01-31T16:43:36Z")

</div>

Correct. You get tso records  
address=“1210 Orange St”, addressable\_id=“5”, addressable\_type=“User”  
address=“1210 Orange St”, addressable\_id=“7”, addressable\_type=“Order”

Basically you’ll probably want to keep them seperate too, because if  
you change a User’s address, why would you change the address of an  
order that’s already been shipped/processed?

-Ryan

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [January 31, 2008, 4:49pm UTC](https://www.ruby-forum.com/t/multiple-relationships-between-two-models/127704/5 "2008-01-31T16:49:06Z")

</div>

Thanks again. I don’t want to duplicate my item records, so I did  
some more searching and found the correct model definitions for what I  
need:

class Contact \< ActiveRecord::Base  
has\_many :artist\_items, :class\_name =\> “Item”, :foreign\_key =\>  
“artist\_id”  
has\_many :buyer\_items, :class\_name =\> “Item”, :foreign\_key =\>  
“buyer\_id”  
def full\_name\_last  
last\_name + ", " + first\_name  
end  
end

class Item \< ActiveRecord::Base  
belongs\_to :artists, :class\_name =\> “Contact”, :foreign\_key =\>  
“artist\_id”  
belongs\_to :buyers, :class\_name =\> “Contact”, :foreign\_key =\>  
“buyer\_id”  
end
