Neal L
2008-Oct-07 15:55 UTC
has_many :through selections based on attributes of the :through table.
Hi all,
I''d like to get your feedback on the easiest way to find records that
are related via has_many :through based on options in the :through
table. Suppose an Author has_many Books :through a table called
Authorships. The Authorships table is defined as:
create_table "authorships", :force => true do |t|
t.belongs_to :author
t.belongs_to :book
t.string "role"
end
The "role" can either be "contributor" or "primary
author". The model
definitions are:
class Author < Active Record::Base
has_many :books, :through => :authorships
end
class Book < ActiveRecord::Base
has_many :authors, :through => :authorships
end
class Authorship < ActiveRecord::Base
belongs_to :author
belongs_to :book
end
So supposing I have a Book, how can I find all of the authors whose
role is defined in the :through table as "contributor"?
Similarly, if I have an author, how can I find all of the books that
the author is the "primary author" of?
Thanks!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Mark James
2008-Oct-07 19:31 UTC
Re: has_many :through selections based on attributes of the :through table.
Neal L wrote:> Hi all, > > I''d like to get your feedback on the easiest way to find records that > are related via has_many :through based on options in the :through > table. Suppose an Author has_many Books :through a table called > Authorships. The Authorships table is defined as: > > create_table "authorships", :force => true do |t| > t.belongs_to :author > t.belongs_to :book > t.string "role" > end > > The "role" can either be "contributor" or "primary author". The model > definitions are: > > class Author < Active Record::Base > has_many :books, :through => :authorships > end > > class Book < ActiveRecord::Base > has_many :authors, :through => :authorships > end > > class Authorship < ActiveRecord::Base > belongs_to :author > belongs_to :book > end > > > So supposing I have a Book, how can I find all of the authors whose > role is defined in the :through table as "contributor"?book.authors.find :all, :conditions => {''authorships.role'' => ''contributor''}> Similarly, if I have an author, how can I find all of the books that > the author is the "primary author" of?author.books.find :all, :conditions => {''authorships.role'' => ''primary author''} -- Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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 -~----------~----~----~----~------~----~------~--~---