Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.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 根据memeber';年代_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 根据memeber';年代

Ruby on rails 根据memeber';年代,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我有一个具有属性:age的模型用户,还有一个具有多个::用户的模型团队 我想知道,如果团队的年龄相同或不同,我如何创建两个作用域:相同年龄和不同年龄来筛选团队 Team.same_ages # In all teams returned, all members have the same age Team.different_ages # In all teams returned, there is at least one member with # different age as t

我有一个具有属性
:age
的模型
用户
,还有一个具有多个::用户的模型
团队

我想知道,如果团队的年龄相同或不同,我如何创建两个作用域:相同年龄和不同年龄来筛选团队

Team.same_ages
# In all teams returned, all members have the same age

Team.different_ages
# In all teams returned, there is at least one member with
# different age as the others

我怎样才能做到这一点呢?

我只是觉得范围会是这样的:

scope :same_ages, -> {
   where("id IN (SELECT team_id
                 FROM users
                 GROUP BY team_id 
                 HAVING COUNT(DISTINCT(age)) = 1)"
}

scope :different_ages, -> {
   where("id IN (SELECT team_id
                 FROM users
                 GROUP BY team_id 
                 HAVING COUNT(DISTINCT(age)) > 1)"
}