We have models A, B, C such that A has_many B :through C Given a in A, b in B, is there a direct way to ask for the cs in C that belong to both without using explicit IDs in AR calls? I got a.cs & b.cs but involves two queries. -- fxn --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Xavier Noria wrote:> We have models A, B, C such that > > A has_many B :through C > > Given a in A, b in B, is there a direct way to ask for the cs in C > that belong to both without using explicit IDs in AR calls? I got > > a.cs & b.cs > > but involves two queries. > > -- fxnIs gather the query you are after is something like select * from C where a_id = :aid and b_id = :bid Why would you want to avoid using the id''s? Stephan -- 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 Jan 17, 2007, at 8:28 PM, Stephan Wehner wrote:> Xavier Noria wrote: >> We have models A, B, C such that >> A has_many B :through C >> Given a in A, b in B, is there a direct way to ask for the cs in C >> that belong to both without using explicit IDs in AR calls? I got >> a.cs & b.cs >> but involves two queries. >> -- fxn > > Is gather the query you are after is something like > > select * from C where a_id = :aid and b_id = :bid > > Why would you want to avoid using the id''s?Because AR is designed in such a way that using an _id column explicitely for simple things triggers a warning in my head, since it usually means I am not using the abstractions correctly. We can here use #detect on a.cs, or a dynamic finder a.cs.find_by_b_id (b.id), but looks non-idiomatic, so I asked whether I am missing something. Perhaps in this particular case there is no prepackaged way to do this, in which case I''ll remove the auto-warning :-). -- fxn --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---