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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheng <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
David
2008-Jan-31 15:33 UTC
Polymorphic Associations (was Multiple relationships between two models)
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 On Jan 31, 10:17 am, kopf1988 <kopf1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssoc... > > 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 >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
kopf1988
2008-Jan-31 15:38 UTC
Re: Polymorphic Associations (was Multiple relationships between two models)
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 On Jan 31, 9:33 am, David <davidtravel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On Jan 31, 10:17 am, kopf1988 <kopf1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > >http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssoc... > > > 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- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Jan 31, 10:38 am, kopf1988 <kopf1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On Jan 31, 9:33 am, David <davidtravel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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 >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---