How to query across several tables?

Hi there,

In the model situation “Book :has_many Stories”, I would like to do
something like this:

Book.find(:all, :conditions => { book.story.id => @story_ids })

…which of course doesn’t work with “book.story.id =>…”
(-> “undefined local variable or method ‘book’”)

What’s the correct query syntax for such a case (where I query across
the 2 tables ‘Books’ and ‘Stories’)?

Thank you very much for your help with this!
Tom

Tom Ha wrote:

What’s the correct query syntax for such a case (where I query across
the 2 tables ‘Books’ and ‘Stories’)?

The magic you are looking for is :include

On 4 Nov 2008, at 16:38, Andrew P. wrote:

(-> “undefined local variable or method ‘book’”)

What’s the correct query syntax for such a case (where I query across
the 2 tables ‘Books’ and ‘Stories’)?

The magic you are looking for is :include

Actually it’s :joins. That’s something of a misuse of :include.

The keys in the hash should be qualified column names (eg :conditions
=> {‘stories.id’ => @story_ids}).
:joins can either be an sql fragment or an association name (or array
or hash of those)

Fred

Got it working like that - thanks very much!