I have a "Product has_many :sales" relationship in my app, and I need to find all Product objects that have no associated Sale objects. Normally I could just find on a null foreign key, but that wont work with a has_many since the foreign key is stored in the associated object. So what does my :conditions need to be find all items that have zero items in a has_many association? Thanks for the help! -- 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 -~----------~----~----~----~------~----~------~--~---
Can this work? Just pseud-code. met = Product.find(:all).inject([]) { |array, product| array << product unless product.sales.any? } However, i think the performance is low.... On 5/24/07, Alex Wayne <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > I have a "Product has_many :sales" relationship in my app, and I need to > find all Product objects that have no associated Sale objects. Normally > I could just find on a null foreign key, but that wont work with a > has_many since the foreign key is stored in the associated object. > > So what does my :conditions need to be find all items that have zero > items in a has_many association? > > Thanks for the help! > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Gabe Da silveira
2007-May-24 04:51 UTC
Re: How do I find records with no associated objects?
Product.find( :all, :select => "DISTINCT products.*", :joins => "LEFT JOIN sales ON products.id = sales.product_id", :conditions => "sales.id IS NULL") This would have to be a bit different if eager loading was involved. Alex Wayne wrote:> I have a "Product has_many :sales" relationship in my app, and I need to > find all Product objects that have no associated Sale objects. Normally > I could just find on a null foreign key, but that wont work with a > has_many since the foreign key is stored in the associated object. > > So what does my :conditions need to be find all items that have zero > items in a has_many association? > > Thanks for the help!-- 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 -~----------~----~----~----~------~----~------~--~---
You will need to use a subquery in the condition that tests for not exists(foreign_key_in_other_table = this_table.id) Michael On May 23, 7:41 pm, Alex Wayne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have a "Product has_many :sales" relationship in my app, and I need to > find all Product objects that have no associated Sale objects. Normally > I could just find on a null foreign key, but that wont work with a > has_many since the foreign key is stored in the associated object. > > So what does my :conditions need to be find all items that have zero > items in a has_many association? > > Thanks for the help! > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Gabe Da silveira wrote:> Product.find( :all, > :select => "DISTINCT products.*", > :joins => "LEFT JOIN sales ON products.id = sales.product_id", > :conditions => "sales.id IS NULL") > > This would have to be a bit different if eager loading was involved. > > Alex Wayne wrote: >> I have a "Product has_many :sales" relationship in my app, and I need to >> find all Product objects that have no associated Sale objects. Normally >> I could just find on a null foreign key, but that wont work with a >> has_many since the foreign key is stored in the associated object. >> >> So what does my :conditions need to be find all items that have zero >> items in a has_many association? >> >> Thanks for the help!Thank you. That seems to work perfectly, although I have no idea why. TIme to brush up on some SQL I think. -- 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 -~----------~----~----~----~------~----~------~--~---