On Mar 18, 5:02 pm, Avi Ir <[email protected]> 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
> Any suggestions for clearer code welcome, but thank you for your help!
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---