I have three models:
class User < ActiveRecord::Base
has_many :rosterplayers
has_many :rosters, -> { uniq } , :through => :rosterplayers
end
class Roster < ActiveRecord::Base
has_many :rosterplayers
has_many :users, -> { uniq }, through: :rosterplayers
end
class Rosterplayer < ActiveRecord::Base
belongs_to :roster
belongs_to :user
validates :user_id, :uniqueness => { :scope => :roster_id }
end
The Rosterplayer table has three columns: user_id
, roster_id,
and pending
(boolean)
Question: Given a roster, how would I retrieve all users that are currently pending?
Attempt: My first attempt was to loop through all the users in the roster:
@team.rosters[0].users.each do |u|
Rosterplayer.find_by(roster_id: rosters[0].id, user_id: u.id, pending: true)
end
But I feel like there is a better way of doing it.