Tom Prats
Tom Prats

Reputation: 7921

Check if record doesn't belong to user

In my index, I want to display all of the Game records. I want to split them up into ones that are already associated with the current user, and ones that are not.

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

I'm wondering if this type of query exists, or if there is a better way to do it.

Upvotes: 0

Views: 159

Answers (2)

scaryguy
scaryguy

Reputation: 7950

If you were mean to use Rails 4, you could do it just using new API called where.not. Like this:

Game.where.not(game: SOMETHING)

Use fresh Rails :)

Upvotes: 1

vee
vee

Reputation: 38645

Try this:

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

Or:

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

Notice the use of parenthesis when you use not in or in operators.

Upvotes: 2

Related Questions