Mike D
Mike D

Reputation: 207

Arel complex query

So i have the following tables

class Game
  belongs_to :player0, :class => "Player"
  belongs_to :player1, :class => "Player"
end

class Player
  belongs_to :user
end


class User
  #has a field called race
end

Now i need a query to do the following

Find all Games where player0.user.race is "x" and player1.user.race is "y" OR player0.user.race is "y" and player1.user.race is "x".

I just began using rails 3. I can easily write the query myself in sql, but i'd rather learn the arel way.

Upvotes: 1

Views: 229

Answers (1)

Mark Huk
Mark Huk

Reputation: 2380

Using meta_where gem:

Game.joins(:player0, :player1).where((:player0 => {:user => {:race => 'x'} | {:race => 'y'}}) | (:player1 => {:user => {:race => 'x'} | {:race => 'y'}}))

Check it by using to_sql method.

Upvotes: 1

Related Questions