Hi How can I translate this sql using find(*args) in activerecord? select x.*, y.*, count(distinct y.name) as name_count from x,y where x.id1 = y.id2 group by y.name order by name_count X.find(:all, :select => "x.*, y.*, count(distinct y.name) as name_count", :include => "y", :group => "y.name", :order => "name_count") doesn''t work. Could you please give me some advice? Thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Don''t use :include with :select, they''re incompatible since :include will do a ton of weird stuff to your select statement. You probably should use a join as well: X.find(:all, :select => "x.*, y.*, count(distinct y.name) as name_count", :joins => "INNER JOIN #{Y.table_name} ON x.id1 = x.id2", :group => "y.name", :order => "name_count") On Mar 19, 11:50 am, "jack.tang" <him...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > > How can I translate this sql using find(*args) in activerecord? > > select x.*, y.*, count(distinct y.name) as name_count > from x,y > where x.id1 = y.id2 > group by y.name > order by name_count > > X.find(:all, > :select => "x.*, y.*, count(distinct y.name) as name_count", > :include => "y", > :group => "y.name", > :order => "name_count") > > doesn''t work. Could you please give me some advice? > > Thanks--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks eden. It works! On Mar 19, 1:45 pm, "eden li" <eden...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Don''t use :include with :select, they''re incompatible since :include > will do a ton of weird stuff to your select statement. > > You probably should use a join as well: > > X.find(:all, > :select => "x.*, y.*, count(distinct y.name) as name_count", > :joins => "INNER JOIN #{Y.table_name} ON x.id1 = x.id2", > :group => "y.name", > :order => "name_count") > > On Mar 19, 11:50 am, "jack.tang" <him...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi > > > How can I translate this sql using find(*args) in activerecord? > > > select x.*, y.*, count(distinct y.name) as name_count > > from x,y > > where x.id1 = y.id2 > > group by y.name > > order by name_count > > > X.find(:all, > > :select => "x.*, y.*, count(distinct y.name) as name_count", > > :include => "y", > > :group => "y.name", > > :order => "name_count") > > > doesn''t work. Could you please give me some advice? > > > Thanks--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---