Advanced AR: select+include OR manual eager load OR virtual-column with assoc eager load SQL

Hi there,

I figured out, that .includes() kills .select(). That means you can not
specify virtual column, like select(‘SUM(points) AS points’) with eager
loading.

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’)

Suggestion:

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.

Thanks hackers,

rndrfero

On Tuesday, July 17, 2012 1:39:54 PM UTC+1, Ruby-Forum.com User wrote:

Hi there,

I figured out, that .includes() kills .select(). That means you can not
specify virtual column, like select(‘SUM(points) AS points’) with eager
loading.

So how you can accomplish this task? Eager loading + select ?

Do you really need eager load or is preloading ok. preloading doesn’t
conflict with select.

A long long time ago I wrote a plugin to tack this onto rails 2.x but
obviously none of that applies with rails 3. It looks to me that the
method
to poke at would be
construct_relation_for_association_find(join_dependency)

Fred