# A -\> B -\> C... A has\_many C's?

**URL:** <https://www.ruby-forum.com/t/a-b-c-a-has-many-cs/146274>\
**Category:** Rails\
**Created:** [August 19, 2008, 6:16pm UTC](https://www.ruby-forum.com/t/a-b-c-a-has-many-cs/146274 "2008-08-19T18:16:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 19, 2008, 6:16pm UTC](https://www.ruby-forum.com/t/a-b-c-a-has-many-cs/146274/1 "2008-08-19T18:16:00Z")

</div>

Hi… I am sure this is a newb question but, here goes.

I have three models:

Genre - can have many subgenres  
Subgenre - only one genre, can have many books  
Book - can belong to many subgenres

Genre:  
has\_many Subgenres

Subgenres:  
belongs\_to genre  
has\_many books\_subgenres  
has\_many books, through books\_subgenres

Book:  
has\_many books\_subgenres  
has\_many subgenres, through books\_subgenres

I have a joining model called Books\_Subgenre

Right now I have no problem getting the book’s subgenres, or the  
subgenres books or the genres subgenres

But I want to get the genres books. How do I do that? Or indeed the  
books genres?

Thanks!

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 19, 2008, 8:02pm UTC](https://www.ruby-forum.com/t/a-b-c-a-has-many-cs/146274/2 "2008-08-19T20:02:51Z")

</div>

Thank Matt… that works, though I have to admit I was hoping for a  
way that would let me use the named\_scopes I have definted on Books.  
e.g.

Genre.first.books.english

But its a start! Thanks!

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 19, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/a-b-c-a-has-many-cs/146274/3 "2008-08-19T18:27:10Z")

</div>

Since Genre has many Subgenres, and a Subgenre has many Books:

genre = Genre.find(id)

# returns an array of the genre’s subgenres

subs = genre.subgenres

# returns an array of all the books in genre

genre\_books = subs.inject([]) { |array, sub| array += sub.books }

Someone might have an easier way to do this but this ought to  
accomplish your purpose. You’d probably want to define this as a  
method in the Genre model so you can do something like:

genre = Genre.find(id)  
books = genre.books

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 20, 2008, 10:09am UTC](https://www.ruby-forum.com/t/a-b-c-a-has-many-cs/146274/4 "2008-08-20T10:09:52Z")

</div>

Do you mean through b?  
If so, that doesn’t seem to work as the relationship between b and c  
is through a joining model

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 20, 2008, 1:54am UTC](https://www.ruby-forum.com/t/a-b-c-a-has-many-cs/146274/5 "2008-08-20T01:54:48Z")

</div>

What about has\_many :cs :through =\> :a?
