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 <[email protected]>

> 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 [email protected].
> To unsubscribe from this group, send email to
> [email protected]<rubyonrails-talk%[email protected]>
> .
> 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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to