Feri z Kosic
2012-Jul-17 12:39 UTC
advanced AR: select+include OR manual eager load OR virtual-column SQL - howto?
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2012-Jul-19 09:50 UTC
Re: advanced AR: select+include OR manual eager load OR virtual-column SQL - howto?
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> 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 > > -- > Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/u71y3ckqn-cJ. For more options, visit https://groups.google.com/groups/opt_out.