johnmcauley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-27 15:20 UTC
Find objects without children
Hello all, I am a newbie and therefore I am currently discovering the delights of Activerecord. I have a collection of objects, some of which have children and some of which have not. I am trying to run a find which will retrieve all parent objects which have children and then return an array. I know there is some sleek way to achieve this, and not the cumbersome iterative approach which looks to be looming ahead. Any help is mucho appreciated... j --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
johnmcauley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> I am a newbie and therefore I am currently discovering the delights of > Activerecord. I have a collection of objects, some of which have > children and some of which have not. I am trying to run a find which > will retrieve all parent objects which have children and then return > an array. I know there is some sleek way to achieve this, and not the > cumbersome iterative approach which looks to be looming ahead.Parent.find :all, :select => ''unique parents.*'', :joins => ''inner join children on children.parent_id = parents.id'' -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
johnmcauley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-28 10:44 UTC
Re: Find objects without children
Thanks for you reply, I didn''t need to use a join I just ran the SQL through Parent.find_by_sql(''SELECT parents.id, parents.name FROM parents, children WHERE parents.id = parents.country_id'') I was hoping to use Active record instead of applying direct SQL but thems the breaks. Laters, j On Mar 27, 7:14 pm, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> johnmcau...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > I am a newbie and therefore I am currently discovering the delights of > > Activerecord. I have a collection of objects, some of which have > > children and some of which have not. I am trying to run afindwhich > > will retrieve allparentobjects which have children and then return > > an array. I know there is some sleek way to achieve this, and not the > > cumbersome iterative approach which looks to be looming ahead. > > Parent.find:all, :select => ''unique parents.*'', > :joins => ''inner join children on children.parent_id = parents.id'' > > -- > We develop, watch us RoR, in numbers too big to ignore.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
johnmcauley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> I didn''t need to use a join I just ran the SQL through > > Parent.find_by_sql(''SELECT parents.id, parents.name FROM parents, > children WHERE parents.id = parents.country_id'')The comma is just syntactic sugar for an inner join. -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---