# How to do queries for 'nested :partial' structures?

**URL:** <https://www.ruby-forum.com/t/how-to-do-queries-for-nested-partial-structures/162618>\
**Category:** Rails\
**Created:** [March 12, 2009, 1:05am UTC](https://www.ruby-forum.com/t/how-to-do-queries-for-nested-partial-structures/162618 "2009-03-12T01:05:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 12, 2009, 1:05am UTC](https://www.ruby-forum.com/t/how-to-do-queries-for-nested-partial-structures/162618/1 "2009-03-12T01:05:54Z")

</div>

Hi there,

what’s the most resource friendly way to program the following?

In the VIEW, I need to show a ‘nested :has\_many’ structure like this:

# ============= Author1 BookA … ChapterX … Page-i … BookB … ChapterX … Page-i … Author2 …

I assume this means that:

- Author, Book, Chapter, Page must be “collections” (@authors, @books,  
@chapters, @pages)
- I need to have a :partial for each collection
- Each :partial contains the next :partial (nested parent-child  
structure)

My question:

- Doing (for example)Â the query for @books directly within the ‘author’  
:partial (and so on) would be stupid, wouldn’t it…
- So how do I need to write the database queries in the controller (in  
the most efficient way) for such a case?
- How would this look like in the view (how to access the values)? (for  
example in any child :partial)

Thank you very much for your help!  
Tom

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 12, 2009, 4:08am UTC](https://www.ruby-forum.com/t/how-to-do-queries-for-nested-partial-structures/162618/2 "2009-03-12T04:08:29Z")

</div>

On Mar 11, 2009, at 8:05 PM, Tom Ha wrote:

> …  
> Author2  
> My question:
> 
> - Doing (for example) the query for @books directly within the  
> ‘author’  
> :partial (and so on) would be stupid, wouldn’t it…
> - So how do I need to write the database queries in the controller (in  
> the most efficient way) for such a case?

@authors = Author.find(:all, :include =\> { :books =\> { :chapters  
=\> :pages }})

Of course, that’s going to instantiate all the authors, books,  
chapters, and pages into objects in your program…

> - How would this look like in the view (how to access the values)?  
> (for  
> example in any child :partial)
> 
> ## Thank you very much for your help! Tom

In your main view,

\<%= render :partial =\> ‘author’, :collection =\> @authors %\>

In the \_author.html.erb

\<%= render :partial =\> ‘book’, :collection =\> author.books %\>

etc.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 12, 2009, 4:51pm UTC](https://www.ruby-forum.com/t/how-to-do-queries-for-nested-partial-structures/162618/3 "2009-03-12T16:51:43Z")

</div>

Really cool! Thanks a lot, Rob!
