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