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)"
}