Jason Matthew
2008-Feb-12 23:31 UTC
Find all models that do not have any associated models
Recipes has many flavors Flavor belongs_to Recipe How would I find and list all recipes that do not have any associated flavors? I am running a nightly report that is looking for all recipes which are missing flavors to let the user know that they neeed to create some flavors for those recipes. Thanks! -- 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 -~----------~----~----~----~------~----~------~--~---
I don''t have a direct answer, but a general solution to that problem would be with validations in the model. -Thufir --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Feb-13 08:52 UTC
Re: Find all models that do not have any associated models
On 12 Feb 2008, at 23:31, Jason Matthew wrote:> > Recipes has many flavors > Flavor belongs_to Recipe > > How would I find and list all recipes that do not have any associated > flavors? > > I am running a nightly report that is looking for all recipes which > are > missing flavors to let the user know that they neeed to create some > flavors for those recipes.The sql you need is left outer join, ie something like Recipe.find :all, :joins => ''left outer join flavours on recipe_id = recipes.id'', :conditions => ''flavours.id is null'', :select => ''recipes.*'' (the :select clause is unneeded on rails 2.0 - rails adds it for you) Fred> > > Thanks! > -- > 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 -~----------~----~----~----~------~----~------~--~---