I''m trying to find all "apples" that belong to user_id 2. But the "apples" table doesn''t have a "user_id" column; it has a "fruit_id" column, that points to a "fruit" object. The "fruit" table has a "user_id" column. So how would I set something up like this to find all the "apples" that belong to user_id 2? @apples = Apple.find(:all, :conditions => ["fruit.user_id = 2"]) -- 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 -~----------~----~----~----~------~----~------~--~---
Joe Peck wrote:> I''m trying to find all "apples" that belong to user_id 2. > > But the "apples" table doesn''t have a "user_id" column; it has a > "fruit_id" column, that points to a "fruit" object. > > The "fruit" table has a "user_id" column. > > So how would I set something up like this to find all the "apples" that > belong to user_id 2? > > @apples = Apple.find(:all, :conditions => ["fruit.user_id = 2"])Just in case someone''s wondering, I already have the right connections in the models (like :belongs_to, :has_many). So that''s not the problem. -- 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 -~----------~----~----~----~------~----~------~--~---
On 1 Nov 2007, at 19:09, Joe Peck wrote:> > Joe Peck wrote: >> I''m trying to find all "apples" that belong to user_id 2. >> >> But the "apples" table doesn''t have a "user_id" column; it has a >> "fruit_id" column, that points to a "fruit" object. >> >> The "fruit" table has a "user_id" column. >> >> So how would I set something up like this to find all the "apples" >> that >> belong to user_id 2? >> >> @apples = Apple.find(:all, :conditions => ["fruit.user_id = 2"]) > > Just in case someone''s wondering, I already have the right connections > in the models (like :belongs_to, :has_many). So that''s not the > problem.Well with a has_many :through you could just do User.find(2).apples If you don''t want to do that then you''ll have to write down the appropriate join. 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 -~----------~----~----~----~------~----~------~--~---
On 11/1/07, Joe Peck <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I''m trying to find all "apples" that belong to user_id 2. > > But the "apples" table doesn''t have a "user_id" column; it has a > "fruit_id" column, that points to a "fruit" object. > > The "fruit" table has a "user_id" column. > > So how would I set something up like this to find all the "apples" that > belong to user_id 2? > > @apples = Apple.find(:all, :conditions => ["fruit.user_id = 2"])You need to join the fruits table to the query. One way to do this is @apples = Apple.find(:all, :include => :fruits, ::conditions => ["fruit.user_id = 2"]) You could also use a :joins string to manual add the sql join clause, but this has the side effect of making the resultant Apple read only since it will contain bogus fields from the fruit table. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---