convert complex SQL to ActiveRecord and Arel


I have a fairly complex SQL statement I'd like to convert to AREL

SELECT count(, players.*
FROM clubs
INNER JOIN players ON players.club_id =
INNER JOIN rankings ON rankings.player_id =
INNER JOIN tournaments ON rankings.tournament_id =
LEFT OUTER JOIN matches ON (matches.p1_id = OR
matches.p2_id =
AND = 7
AND = 19

How would I do this?

One step at a time ...

Even though you have the query Club-centric, you're asking for players.* so I'd start with the Player model:'count( as match_count, players.*')

Then you have inner joins:
       .joins([:club, { :rankings => :tournament }])

And then an outer join (this is a tricky one and depends on how you've defined the associations):

Looks like you already know the club and the tournament:
       .where([' = ? AND = ?', 7, 19])

And you want the count() function to behave:

Then ask for all of 'em:

That may not actually work, but it certainly ought to give you some hints. (And there's probably other ways to get the same information, but you need to ask a better question to get a better answer.)


Rob Biedenharn

Hi Rob,

I really appreciate your feedback.

One of the things I;m struggling with is to fetch a player's matches
with Arel:

irb) Player.joins(:matches)
ActiveRecord::ConfigurationError: Association named 'matches' was not
found; perhaps you misspelled it?

A match does not have a player_id it does have p1_id and p2_id.
Hence the "JOIN matches ON (matches.p1_id = OR
matches.p2_id =" in SQL

I'm already struggling to get a Player#has_many :matches going. I can
do it the old fashioned way using finder_sql but that doesn't use Arel
so it hard to chain more things to it.

I dont know how to specify the join columns in in a Player.joins(...)

Sorry if my questions aren't clear, I just don't have a clue where to
start, Arel docs & examples on this topic seem hard to find.



Well, you're discovering that SQL is hard for relations that aren't symmetric (i.e., your p1_id and p2_id). If you add a MatchPlayer model, you can probably simplify your SQL and if you keep a 'position' you can still maintain which is "Player 1" and which is "Player 2" in a match. The bare minimum is probably something like:







And thus in AR:
   belongs_to :club
   has_many :match_players
   has_many :matches, :through => :match_players
   belongs_to :tournament'count( as match_count, players.*')
       .includes([:club, { :matches => :tournament }])
       .where([' = ? AND = ?', 7, 19])

I don't think that this query needs Ranking at all (although it was probably used to create the Match and MatchPlayer records).


Rob Biedenharn