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 -- Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tom Ha wrote:> 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'')?The magic you are looking for is :include --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 4 Nov 2008, at 16:38, Andrew Porter wrote:> > Tom Ha wrote: >> 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'')? > > The magic you are looking for is :includeActually 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> > > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Got it working like that - thanks very much! -- Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---