# Rails Query within Query

**URL:** <https://www.ruby-forum.com/t/rails-query-within-query/162714>\
**Category:** Rails\
**Created:** [March 13, 2009, 1:24am UTC](https://www.ruby-forum.com/t/rails-query-within-query/162714 "2009-03-13T01:24:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aviir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@aviir](https://www.ruby-forum.com/u/aviir)\
**Post date:** [March 13, 2009, 1:24am UTC](https://www.ruby-forum.com/t/rails-query-within-query/162714/1 "2009-03-13T01:24:29Z")

</div>

Hello,

I am looking for a better way to do the following (my code examples are  
crappy pseudocode):

I have two associated models, Book and Chapter, where book has\_many  
chapters. I want to first select several books, for example,

books\_by\_dr\_seuss = Book.find(:all, :conditions =\> {:author =\> “Seuss”  
})

Then, I want to select all the “Chapter 3”'s from this result, i.e.  
Chapter 3 of Cat in the Hat, Chapter 3 of Fox in Socks, etc.

I want to do something like:

chapter\_threes = Chapter.find(:all, :include=\>[:book], :conditions =\>  
{chapter.book in books\_by\_dr\_seuss } )

How would I do this without resorting to something like “select \* from  
chapters where chapter.book\_id=1 or chapter.book\_id=2, etc”?

Thanks for your help.  
A

---

<div class="post-metadata">

**Author:** ![aviir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@aviir](https://www.ruby-forum.com/u/aviir)\
**Post date:** [March 13, 2009, 1:32am UTC](https://www.ruby-forum.com/t/rails-query-within-query/162714/2 "2009-03-13T01:32:18Z")

</div>

Wouldn’t it be something like the following:

suess\_chapter\_three = Chapter.find(:all, :include=\>:book, :conditions =\>  
[‘books.auther like “suess” and chapters.chapter = 3’])

Simon

---

<div class="post-metadata">

**Author:** ![aviir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@aviir](https://www.ruby-forum.com/u/aviir)\
**Post date:** [March 13, 2009, 1:54am UTC](https://www.ruby-forum.com/t/rails-query-within-query/162714/3 "2009-03-13T01:54:30Z")

</div>

I think something like the following:

book\_ids = ids of selected books (as gleened from the controller)  
chapter\_threes = Chapter.find(:all, :include =\> :book, :conditions =\>  
[‘book.id in ? and chapter.chapter\_number = 3’, book\_ids])

obviously not tested, but once you fix any syntax errors it should work.

Simon

---

<div class="post-metadata">

**Author:** ![aviir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@aviir](https://www.ruby-forum.com/u/aviir)\
**Post date:** [March 13, 2009, 1:46am UTC](https://www.ruby-forum.com/t/rails-query-within-query/162714/4 "2009-03-13T01:46:14Z")

</div>

Simon M. wrote:

> Wouldn’t it be something like the following:
> 
> suess\_chapter\_three = Chapter.find(:all, :include=\>:book, :conditions =\>  
> [‘books.auther like “suess” and chapters.chapter = 3’])
> 
> Simon

Thanks for the response Simon – I think my example didn’t fully capture  
what I am trying to do 🙂

I want to select from a collection or list of items. I can’t use  
‘books.author like’ because there is no criteria that ties them all  
together.

A better example would be:  
User is presented with a list of checkboxes (let’s say, book names) in a  
form. He checks several of these checkboxes. The form gets submitted  
and returns all of the chapters 3 from the selected books.

For example say the user selects:

Fox in Sock by Dr Seuss  
War and Peace by Leo Tolstoy  
The Godfather by Mario Puzo

The form passes the ids of these selections (let’s say, [1000, 1001,  
1002]) to the controller, which then calls a query on these selected  
plates:

selected\_books = params[:selected\_books]  
Chapter.find(:all, :include=\>:book, :conditions =\> ‘chapter.book\_id =  
book.id and book.id in books and chapters.chapter=3’ )

Is there a way to do that statement? “in books” is obviously not a  
valid command…

Thanks!  
A

---

<div class="post-metadata">

**Author:** ![aviir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@aviir](https://www.ruby-forum.com/u/aviir)\
**Post date:** [March 18, 2009, 6:02pm UTC](https://www.ruby-forum.com/t/rails-query-within-query/162714/5 "2009-03-18T18:02:06Z")

</div>

Thank you for the reply Simon. What you said worked for me, although I  
did it in a rather inelegant way – I’m not sure about the correct  
syntax of my solution, but it works. It requires creating a string with  
the ids in parens…

book\_ids = “(100, 101, 102)”  
chapters= Chapter.find(:all, :include =\> :books, :conditions =\>  
[“books.id in #{book\_ids} and chapter=‘2’”]

Any suggestions for clearer code welcome, but thank you for your help!

---

<div class="post-metadata">

**Author:** ![aviir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@aviir](https://www.ruby-forum.com/u/aviir)\
**Post date:** [March 18, 2009, 6:37pm UTC](https://www.ruby-forum.com/t/rails-query-within-query/162714/6 "2009-03-18T18:37:17Z")

</div>

On Mar 18, 5:02 pm, Avi Ir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you for the reply Simon. What you said worked for me, although I  
> did it in a rather inelegant way – I’m not sure about the correct  
> syntax of my solution, but it works. It requires creating a string with  
> the ids in parens…
> 
> book\_ids = “(100, 101, 102)”  
> chapters= Chapter.find(:all, :include =\> :books, :conditions =\>  
> [“books.id in #{book\_ids} and chapter=‘2’”]

first off that can be done nicer: book\_ids = [1,2,3]

chapters= Chapter.find(:all, :include =\> :books, :conditions =\>  
[“books.id in (?) and chapter=‘2’”, book\_ids]

or even

chapters= Chapter.find(:all, :include =\> :books, :conditions =\>  
{ ‘books.id’ =\> book\_ids, ‘chapter’ =\> 2})

You don’t even need to get an array of book\_ids - an array of  
activerecord objects will do fine too.

And last of all:

Chapter.find :all, :joins =\> :books, :conditions =\> {‘books.author’ =\>  
‘Seuss’, ‘chapter’ =\> 2}

Fred

---

<div class="post-metadata">

**Author:** ![aviir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@aviir](https://www.ruby-forum.com/u/aviir)\
**Post date:** [March 18, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/rails-query-within-query/162714/7 "2009-03-18T18:59:24Z")

</div>

Hi Fred,

Thanks for your response – Your second example is exactly what I  
needed.

The final example, though – why do you use :joins instead of :include?
