kjs*_*js3 37 activerecord ruby-on-rails ruby-on-rails-3
我从代码的另一部分有一系列团队名称,我想找到不在该数组中的所有团队.我尝试过以下内容并不起作用.
@team_exclude_list = ['Team 1', 'Team 2', 'Team 3']
@teams = Team.where("name != ?", @team_exclude_list)
这是在Rails 3中,Googles并没有给我太多的爱.
nsl*_*cum 67
Rails 4解决方案:
@team_exclude_list = ['Team 1', 'Team 2', 'Team 3']
@teams = Team.where.not(name: @team_exclude_list)
此外,要加快查询速度,您可以:
要么
Sté*_*hen 63
我从来没有使用字符串字段,但也许这将工作:
@teams = Team.where("name NOT IN (?)", @team_exclude_list)
| 归档时间: | 
 | 
| 查看次数: | 22407 次 | 
| 最近记录: |