Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Mongoid:与alias有很多关系_Ruby On Rails_Ruby On Rails 3_Mongodb_Mongoid - Fatal编程技术网

Ruby on rails Mongoid:与alias有很多关系

Ruby on rails Mongoid:与alias有很多关系,ruby-on-rails,ruby-on-rails-3,mongodb,mongoid,Ruby On Rails,Ruby On Rails 3,Mongodb,Mongoid,背景:我有一个团队模型,它有许多玩家,允许一个人打电话 @team.players 并接收Mongoid::Relations::Targets::可枚举的玩家列表 目标:我还希望能够检索团队中特定位置的球员名单。例如,如果一个用户在他的团队中添加了一个投手,我就可以给他打电话 @team.pitchers返回可枚举的投手列表。有没有关于如何设置的想法?在mongoid中无法设置条件 我有两种方法可以考虑这样做:在玩家中设置一个范围,并使用@team.players.pitchers进行呼叫

背景:我有一个团队模型,它有许多玩家,允许一个人打电话

@team.players 
并接收Mongoid::Relations::Targets::可枚举的玩家列表

目标:我还希望能够检索团队中特定位置的球员名单。例如,如果一个用户在他的团队中添加了一个投手,我就可以给他打电话
@team.pitchers
返回可枚举的投手列表。有没有关于如何设置的想法?

在mongoid中无法设置条件

我有两种方法可以考虑这样做:在玩家中设置一个范围,并使用@team.players.pitchers进行呼叫

Class Player
  scope :pitchers, where(:position => "pitcher")
end
或者在团队中定义一个方法

Class Team

  def pitchers
    self.players.where(:position => "pitcher")
  end
end