检查记录是否不属于用户

时间:2013-08-10 20:06:56

标签: ruby-on-rails ruby-on-rails-3 activerecord

在我的索引中,我想显示所有游戏记录。我想将它们分成已经与当前用户关联的那些,以及那些不与当前用户关联的那些。

def index
  @games = current_user.games
  @others = Game.where(game not in @games)  # how do i do this?
end

我想知道这种类型的查询是否存在,或者是否有更好的方法。

2 个答案:

答案 0 :(得分:2)

试试这个:

@others = Game.where('id not in (:games)', games: @games)

或者:

@others = Game.where('id not in (?)', @games)

请注意在使用not inin运算符时使用括号。

答案 1 :(得分:1)

如果你打算使用Rails 4,你可以使用名为where.not的新API。像这样:

Game.where.not(game: SOMETHING)

使用新鲜的Rails :)

相关问题