Bob Sanders
2009-Feb-17 01:36 UTC
How do you check if associated table is empty from find?
So I have two tables: - teams - players I''m trying to find all teams that have no players. I thought this could work: Team.find(:all, :joins => :players, :conditions => ["players.size = ?", 0] ) Of course that''s not working for me because I''m mixing the SQL with Rails. Do you know the proper way to find all teams that have no players? -- 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 -~----------~----~----~----~------~----~------~--~---
jemminger
2009-Feb-17 01:46 UTC
Re: How do you check if associated table is empty from find?
You could add a :counter_cache to your model if you want a simple solution: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html On Feb 16, 8:36 pm, Bob Sanders <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> So I have two tables: > > - teams > - players > > I''m trying to find all teams that have no players. I thought this could > work: > > Team.find(:all, :joins => :players, :conditions => ["players.size = ?", > 0] ) > > Of course that''s not working for me because I''m mixing the SQL with > Rails. Do you know the proper way to find all teams that have no > players? > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---