Hi, I have these models in my ror application: author, article and book. I want to associate author to article and book so: book has many authors article has many authors author belongs to both of them What''s the best practice for doing so? Should I have both book_id and article_id in author table? Other solutions? Thanks in advance, --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Arshak Navruzyan
2008-Mar-24 02:39 UTC
Re: Associating a resource with multiple resources?
Houman, I think you want a has_many :through relationship. Here is an analogy: http://wiki.rubyonrails.org/rails/pages/Beginner+Howto+on+has_many+:through Arshak Houman Dunnil wrote:> Hi, > > I have these models in my ror application: author, article and book. I > want to associate author to article and book so: > > book has many authors > article has many authors > author belongs to both of them > > What''s the best practice for doing so? Should I have both book_id and > article_id in author table? Other solutions? > > Thanks in advance,-- 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 -~----------~----~----~----~------~----~------~--~---
If you have reasons to stick with the current model, then introduce a fourth concept, call it ''Publishing''. A ''publishing'' is something that an author has written. In this case, it associates an author to a book or an article. Now you can use two techniques -- has_many :through (as suggested by Arshak) and polymorphic associations. create_table :publishing do |t| t.reference :publishable, :polymorphic=>true t.reference :author end class Publishing < ARec::Base belongs_to :author belongs_to :publishable, :polymorphic=>true ... end class Author < ARec::Base has_many :publishings ... end class Book < ARec::Base has_many :publishings, :as=>:publishable has_many :authors, :through=>:publishing ... end class Article < ARec::Base has_many :publishings, :as=>:publishable has_many :authors, :through=>:publishing ... end The other possibility would be to use Single Table Inheritance, putting the books and articles into a single table that uses a discriminator column so that you can tell them apart. You can still use the has_many :through concept (to support 1 or more authors) but you eliminate one table. On Mar 23, 10:17 am, "Houman Dunnil" <h.dun...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I have these models in my ror application: author, article and book. I > want to associate author to article and book so: > > book has many authors > article has many authors > author belongs to both of them > > What''s the best practice for doing so? Should I have both book_id and > article_id in author table? Other solutions? > > Thanks in advance,--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---