# Multiple relationships between two models

**URL:** <https://discuss.rubyonrails.org/t/multiple-relationships-between-two-models/24138>\
**Category:** rubyonrails-talk\
**Created:** [January 31, 2008, 2:59pm UTC](https://discuss.rubyonrails.org/t/multiple-relationships-between-two-models/24138 "2008-01-31T14:59:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [January 31, 2008, 2:59pm UTC](https://discuss.rubyonrails.org/t/multiple-relationships-between-two-models/24138/1 "2008-01-31T14: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| &nbsp;&nbsp;t.column :first\_name, :string &nbsp;&nbsp;t.column :last\_name, :string end

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

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

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

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

&nbsp;&nbsp;def full\_name\_last &nbsp;&nbsp;last\_name + ", " + first\_name &nbsp;&nbsp;end end

class Artist \< Contact &nbsp;&nbsp;has\_many :items end

class Buyer \< Contact &nbsp;&nbsp;has\_many :items end

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

class Item \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :artists &nbsp;&nbsp;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:** ![kopf1988](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@kopf1988](https://discuss.rubyonrails.org/u/kopf1988)\
**Post date:** [January 31, 2008, 3:17pm UTC](https://discuss.rubyonrails.org/t/multiple-relationships-between-two-models/24138/2 "2008-01-31T15:17:52Z")

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

---

<div class="post-metadata">

**Author:** ![David13](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@David13](https://discuss.rubyonrails.org/u/David13)\
**Post date:** [January 31, 2008, 3:33pm UTC](https://discuss.rubyonrails.org/t/multiple-relationships-between-two-models/24138/3 "2008-01-31T15:33:55Z")

</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:** ![kopf1988](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@kopf1988](https://discuss.rubyonrails.org/u/kopf1988)\
**Post date:** [January 31, 2008, 3:38pm UTC](https://discuss.rubyonrails.org/t/multiple-relationships-between-two-models/24138/4 "2008-01-31T15:38:31Z")

</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:** ![David13](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@David13](https://discuss.rubyonrails.org/u/David13)\
**Post date:** [January 31, 2008, 3:48pm UTC](https://discuss.rubyonrails.org/t/multiple-relationships-between-two-models/24138/5 "2008-01-31T15:48:33Z")

</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 &nbsp;&nbsp;has\_many :artist\_items, :class\_name =\> "Item", :foreign\_key =\> "artist\_id" &nbsp;&nbsp;has\_many :buyer\_items, :class\_name =\> "Item", :foreign\_key =\> "buyer\_id" &nbsp;&nbsp;def full\_name\_last &nbsp;&nbsp;&nbsp;&nbsp;last\_name + ", " + first\_name &nbsp;&nbsp;end end

class Item \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :artists, :class\_name =\> "Contact", :foreign\_key =\> "artist\_id" &nbsp;&nbsp;belongs\_to :buyers, :class\_name =\> "Contact", :foreign\_key =\> "buyer\_id" end
