I figured out, that .includes() kills .select(). That means you can not
specify virtual column, like select('SUM(points) AS points') with eager
So how you can accomplish this task? Eager loading + select ?
Ok, this is our case:
We are selecting 'players'. player belongs to 'sportsman', which has
column called 'name', this column we want to eager load in following
statement. We are suming column 'points' from has_many :events
association as our desired virtual column.
Player.joins('LEFT OUTER JOIN events ON players.id =
events.player_id').joins('LEFT OUTER JOIN sportsmen ON
players.sportsman_id = sportsmen.id').select('players.*, sporsmen.name,
SUM(points) AS points').group('players.id').order('sportsmen.name DESC')
Is there some convention what to write to .select() to eager load. If i
write select('players.*, sportsmen.name, SUM(points) AS points), it
loads sportsman.name lazily anyways (so it loads it twice).
Or its possible throught AREL?
Do you understand what I mean? To use eager loading with virtual column.
Thanks, I was fighting with this all the yesterday, learning a lot from
AR, but with still no success in this case.