0
class Game < ActiveRecord::Base
  has_many :game_types, :dependent => :destroy
  has_many :types, :through => :game_types
end

class Type < ActiveRecord::Base
  has_many :game_types, :dependent => :destroy
  has_many :games, :through => :game_types
end

Game Type 1 == Single Player Game Type 2 == Multiplayer

How can I query for Games that have either type IDs 1 OR 2 OR both?

Additionally, how can I query for Games that have neither?

This is being used with pagination via will_paginate so single queries would be preferable.

Thank you in advance for rescuing my sanity.

1 Answers1

3

In rails you can always resort to just simple SQL. OR is not yet implemented fully in Arel (last I checked) so what you want is similar to this:

Game.joins("game_types ON (game_types.game_id = games.id)").where("game_types.id IN (1,2)")

This way the query is still reasonably performant. You can expand this a bit to not include direct references to the Ids, but that's up to you.

Tigraine
  • 23,358
  • 11
  • 65
  • 110