Multiple relationships between two models

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

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

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

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

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