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...
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]