2

BACKGROUND: I have a Team model which has_many Players, which allows for one to call

@team.players 

and receive a Mongoid::Relations::Targets::Enumerable list of Players back.

OBJECTIVE: I'd like to also be able to retrieve a list of players at a specific position on a team. For instance, if a user adds a pitcher to his team, I'd be able to call @team.pitchers to return an Enumerable list of pitchers. Any ideas on how to set this up?

neon
  • 2,811
  • 6
  • 30
  • 44

1 Answers1

1

cant put conditions in has_many in mongoid.

two ways i can think of doing this is set a scope in players and call using @team.players.pitchers

Class Player
  scope :pitchers, where(:position => "pitcher")
end

or define a method in team

Class Team

  def pitchers
    self.players.where(:position => "pitcher")
  end
end
RoRRe
  • 291
  • 2
  • 10