I have a Teams that has_many Players. I need to get the list of Teams that have no active players (whether they have 0 players or they have a lot of players all of whom are non- active). Can''t figure out the conditions for doing this other than subtracting the ones with active players from the list of all teams. The issue is that I don''t want teams with *some* non-active players, I need the teams which have only non-active players. Here''s how I''m doing it now. I assume that there''s a better way so will appreciate seeing the magic. Thanks, -Chris Player < AR::Base validates_inclusion_of :status, :in => [:active, :inactive, :injured, :dead] end Team < AR::Base has_many :players named_scope :with_active_players, { :include => :players, :conditions => ["players.status = ?", "active"], :group => "teams.id", # no dups :order => ''teams.name'' } def self.without_active_players (Team.find(:all) - Team.with_active_players).sort_by { |t| t.name } end end -- 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.
Try: named_scope :without_active_players, :conditions => "NOT EXISTS(SELECT * FROM players WHERE team_id=teams.id AND status=''active'')", :order => "name" Does this work? Lasse 2010/2/14 cpr <crosebrugh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> I have a Teams that has_many Players. > > I need to get the list of Teams that have no active players (whether > they have 0 players or they have a lot of players all of whom are non- > active). > > Can''t figure out the conditions for doing this other than subtracting > the ones with active players from the list of all teams. The issue is > that I don''t want teams with *some* non-active players, I need the > teams which have only non-active players. > > Here''s how I''m doing it now. I assume that there''s a better way so > will appreciate seeing the magic. > > Thanks, > > -Chris > > Player < AR::Base > > validates_inclusion_of :status, :in => > [:active, :inactive, :injured, :dead] > > end > > > Team < AR::Base > > has_many :players > > named_scope :with_active_players, { > :include => :players, > :conditions => ["players.status = ?", "active"], > :group => "teams.id", # no dups > :order => ''teams.name'' > } > > def self.without_active_players > (Team.find(:all) - Team.with_active_players).sort_by { |t| > t.name } > end > > end > > > -- > 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.
He shoots. He scores! Thanks much for the insight. -Chris On Feb 14, 12:15 pm, Lasse Bunk <lasseb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Try: > > named_scope :without_active_players, :conditions => "NOT EXISTS(SELECT * > FROM players WHERE team_id=teams.id AND status=''active'')", :order => "name" > > Does this work? > > Lasse > > 2010/2/14 cpr <crosebr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > > > I have a Teams that has_many Players. > > > I need to get the list of Teams that have no active players (whether > > they have 0 players or they have a lot of players all of whom are non- > > active). > > > Can''t figure out the conditions for doing this other than subtracting > > the ones with active players from the list of all teams. The issue is > > that I don''t want teams with *some* non-active players, I need the > > teams which have only non-active players. > > > Here''s how I''m doing it now. I assume that there''s a better way so > > will appreciate seeing the magic. > > > Thanks, > > > -Chris > > > Player < AR::Base > > > validates_inclusion_of :status, :in => > > [:active, :inactive, :injured, :dead] > > > end > > > Team < AR::Base > > > has_many :players > > > named_scope :with_active_players, { > > :include => :players, > > :conditions => ["players.status = ?", "active"], > > :group => "teams.id", # no dups > > :order => ''teams.name'' > > } > > > def self.without_active_players > > (Team.find(:all) - Team.with_active_players).sort_by { |t| > > t.name } > > end > > > end > > > -- > > 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%2Bunsubscrib e@googlegroups.com> > > . > > 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.