Rick DeNatale
2010-Jun-11 18:26 UTC
How to find models that have at least one associated has_many
I''m having a senior moment. I''ve done this before but I can''t remember how. Two active record models, Foo belongs_to Bar Bar has_many Foos I want to find all Bars that have at least one Foo, is it: Bar.all(:include => :foos, :conditions => "foo.id IS NOT NULL'') or something else? -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- 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.
Bill Walton
2010-Jun-11 18:37 UTC
Re: How to find models that have at least one associated has_many
Hi Rick, On Fri, Jun 11, 2010 at 1:26 PM, Rick DeNatale <rick.denatale-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m having a senior moment. I''ve done this before but I can''t remember how. > > Two active record models, > > Foo belongs_to Bar > > Bar has_many Foos > > I want to find all Bars that have at least one Foo, is it: > > Bar.all(:include => :foos, :conditions => "foo.id IS NOT NULL'')Seems like it would be ''foo.bar_id'' in the condition clause. HTH, Bill -- 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.
Michael Pavling
2010-Jun-11 19:10 UTC
Re: How to find models that have at least one associated has_many
On 11 June 2010 19:26, Rick DeNatale <rick.denatale-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m having a senior moment. I''ve done this before but I can''t remember how. > > I want to find all Bars that have at least one Foo, is it: > > Bar.all(:include => :foos, :conditions => "foo.id IS NOT NULL'') > > or something else?Doesn''t... Bar.all(:joins => :foos).uniq ...do the job? -- 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.
Michael Pavling
2010-Jun-11 19:13 UTC
Re: How to find models that have at least one associated has_many
On 11 June 2010 20:10, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Bar.all(:joins => :foos).uniqSeems to... quick and dirty test on one of my models:>> Person.all(:include => :graduations).select { |person| !person.graduations.empty? }.size=> 87>> Person.all(:include => :graduations).select { |person| person.graduations.size > 0 }.size=> 87>> Person.all(:joins => :graduations).size=> 103>> Person.all(:joins => :graduations).uniq.size=> 87>>-- 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.