# has\_many :through selections based on attributes of the :through 	table.

**URL:** <https://discuss.rubyonrails.org/t/has-many-through-selections-based-on-attributes-of-the-through-table/35126>\
**Category:** rubyonrails-talk\
**Created:** [October 7, 2008, 3:55pm UTC](https://discuss.rubyonrails.org/t/has-many-through-selections-based-on-attributes-of-the-through-table/35126 "2008-10-07T15:55:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neal\_L](https://avatars.discourse-cdn.com/v4/letter/n/74df32/32.png) [@Neal\_L](https://discuss.rubyonrails.org/u/Neal_L)\
**Post date:** [October 7, 2008, 3:55pm UTC](https://discuss.rubyonrails.org/t/has-many-through-selections-based-on-attributes-of-the-through-table/35126/1 "2008-10-07T15:55:17Z")

</div>

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:

&nbsp;&nbsp;create\_table "authorships", :force =\> true do |t| &nbsp;&nbsp;&nbsp;&nbsp;t.belongs\_to :author &nbsp;&nbsp;&nbsp;&nbsp;t.belongs\_to :book &nbsp;&nbsp;&nbsp;&nbsp;t.string "role" &nbsp;&nbsp;end

The "role" can either be "contributor" or "primary author". The model definitions are:

class Author \< Active Record::Base &nbsp;&nbsp;has\_many :books, :through =\> :authorships end

class Book \< ActiveRecord::Base &nbsp;&nbsp;has\_many :authors, :through =\> :authorships end

class Authorship \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :author &nbsp;&nbsp;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!

---

<div class="post-metadata">

**Author:** ![Mark\_James](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mark\_James](https://discuss.rubyonrails.org/u/Mark_James)\
**Post date:** [October 7, 2008, 7:31pm UTC](https://discuss.rubyonrails.org/t/has-many-through-selections-based-on-attributes-of-the-through-table/35126/2 "2008-10-07T19:31:00Z")

</div>

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:
> 
> &nbsp;&nbsp;create\_table "authorships", :force =\> true do |t| &nbsp;&nbsp;&nbsp;&nbsp;t.belongs\_to :author &nbsp;&nbsp;&nbsp;&nbsp;t.belongs\_to :book &nbsp;&nbsp;&nbsp;&nbsp;t.string "role" &nbsp;&nbsp;end
> 
> The "role" can either be "contributor" or "primary author". The model definitions are:
> 
> class Author \< Active Record::Base &nbsp;&nbsp;has\_many :books, :through =\> :authorships end
> 
> class Book \< ActiveRecord::Base &nbsp;&nbsp;has\_many :authors, :through =\> :authorships end
> 
> class Authorship \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :author &nbsp;&nbsp;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, &nbsp;&nbsp;&nbsp;: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, &nbsp;&nbsp;&nbsp;:conditions =\> {'authorships.role' =\> 'primary author'}
