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