【发布时间】:2016-04-30 16:29:40
【问题描述】:
在我的应用程序中,如果存在 2 条具有我/相同条件的记录,我想仅显示 db 记录?
最有效的检查方法是什么?
这是我的代码:
- if @users.where(friend_group: 0).where(city_id: 2).any?
- @users.where(user_group: 0).where(city_id: 2).each do |user|
%p= user.name
%p= user.city.name
而不是any?,我想检查是否只有 2条条件相同的记录存在时,它可以显示它们。
我试过- if @users.where(friend_group: 0).where(city_id: 2).exist?(2),但没有成功。
ps:我使用的是 rails 4.2
【问题讨论】:
-
正好两个或至少两个?如果至少有两条,您是要显示所有记录还是只显示其中两条(按什么顺序)?
标签: ruby-on-rails ruby conditional-statements