Forum: Ruby on Rails advanced AR: select+include OR manual eager load OR virtual-column with assoc eager load SQL

E51b1e21e3de796ecf974cf143e634b5?d=identicon&s=25 Feri z Kosic (rndrfero)
on 2012-07-17 14:39
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2012-07-19 11:51
(Received via mailing list)
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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.