I have LabGroup and LabDesc which have many through LabDescGroup. I''d like to identify LabDescs which don''t have a LabGroup. This works: x = [] LabDescGroup.find( :all, :select => ''DISTINCT lab_desc_id'' ).each{ |c| x << c.lab_desc_id } y = [] LabDesc.find( :all, :select => ''id'' ).each{ |c| y << c.id } notfound = x - y But is there a more elegant way? It feels like I should be able to do this entirely through queries, but I''m not seeing the solution. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Lasse Bunk
2010-Mar-24 21:42 UTC
Re: More elegant solution for "missing" has-many through ids?
This should do it: LabDesc.find(:all, :conditions => ''NOT EXISTS(SELECT * FROM lab_desc_groups WHERE lab_desc_id=lab_descs.id)'') /Lasse 2010/3/24 Dudebot <craignied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> I have LabGroup and LabDesc which have many through LabDescGroup. I''d > like to identify LabDescs which don''t have a LabGroup. This works: > > x = [] > LabDescGroup.find( :all, :select => ''DISTINCT lab_desc_id'' ).each{ |c| > x << c.lab_desc_id } > y = [] > LabDesc.find( :all, :select => ''id'' ).each{ |c| y << c.id } > notfound = x - y > > But is there a more elegant way? It feels like I should be able to do > this entirely through queries, but I''m not seeing the solution. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dudebot
2010-Mar-24 23:10 UTC
Re: More elegant solution for "missing" has-many through ids?
On Mar 24, 4:42 pm, Lasse Bunk <lasseb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This should do it: > > LabDesc.find(:all, :conditions => ''NOT EXISTS(SELECT * FROM > lab_desc_groups WHERE lab_desc_id=lab_descs.id)'')Lasse, you have my "SQL Guru of the Year Award." Many thanks, Craig -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Lasse Bunk
2010-Mar-25 18:14 UTC
Re: Re: More elegant solution for "missing" has-many through ids?
He he he NICE :-) /Lasse 2010/3/25 Dudebot <craignied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> On Mar 24, 4:42 pm, Lasse Bunk <lasseb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > This should do it: > > > > LabDesc.find(:all, :conditions => ''NOT EXISTS(SELECT * FROM > > lab_desc_groups WHERE lab_desc_id=lab_descs.id)'') > > Lasse, you have my "SQL Guru of the Year Award." Many thanks, > Craig > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.