0

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

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

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

4

2 回答 2

2

尝试这个:

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

或者:

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

使用not inorin运算符时请注意括号的使用。

于 2013-08-10T20:08:55.373 回答
1

如果您打算使用 Rails 4,您可以使用名为where.not. 像这样:

Game.where.not(game: SOMETHING)

使用新鲜的 Rails :)

于 2013-08-10T20:14:55.947 回答