<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Inspire Creativity: Rails: How to eager load associations with :select (only load select columns from each table)</title>
    <link>http://kevinwatt.org/articles/2007/04/13/rails-how-to-eager-load-associations-with-select-only-load-select-columns-from-each-table</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Rails: How to eager load associations with :select (only load select columns from each table)</title>
      <description>&lt;p&gt;
I've been avoiding blogging about things that were overly technical for awhile, but screw it, I need more things to write about anyways, so I'll start sharing nuggets of knowledge here as well.
&lt;/p&gt;&lt;p&gt;
Rails eager loading lets you load sub-elements on the database level, which can be a huge speed increase, though sometimes at a database cost.  In my experience, the biggest problem is that items I'm joining together often have a big 'description' column, that can cause memory issues if loaded too indescriminatingly. 
&lt;/p&gt;&lt;p&gt;
&lt;code&gt;@logs = ItemLog.find(:all, :conditions =&amp;gt; "target_id=#{@user.id}", :order =&amp;gt; 'item_logs.created_at DESC', :limit =&amp;gt; 40, :include =&amp;gt; 'item') &lt;/code&gt;  
&lt;/p&gt;&lt;p&gt;
What I wanted to do was add :select =&amp;gt; 'only_rows_i_want'.  But rails doesn't let you use :select with :include.
&lt;/p&gt;&lt;p&gt;
So what I did was do it without the :select, and take a look at the log file to see what the generated query would be.  Then I removed the items I wanted and used it as a find_by_sql query:
&lt;/p&gt;&lt;p&gt;
&lt;code&gt;rows = ItemLog.find_by_sql("SELECT item_logs.`id` AS t0_r0, item_logs.`user_id` AS t0_r1, item_logs.`item_id` AS t0_r2, item_logs.`created_at` AS t0_r3, item_logs.`referer` AS t0_r4, item_logs.`target_id` AS t0_r5, items.`id` AS t1_r0, items.`user_id` AS t1_r1, items.`title` AS t1_r4 FROM item_logs LEFT OUTER JOIN items ON items.id = item_logs.item_id WHERE (target_id=#{@user.id}) ORDER BY item_logs.created_at DESC LIMIT 50")&lt;/code&gt;
&lt;/p&gt;&lt;p&gt;
Then how do you get it to make the sub-elements into the actual objects?
&lt;/p&gt;&lt;p&gt;
&lt;code&gt;join_dep = ActiveRecord::Associations::ClassMethods::JoinDependency.new(ItemLog, ['item'], nil)    
&lt;br /&gt;@logs = join_dep.instantiate(rows) &lt;/code&gt;
&lt;/p&gt;&lt;p&gt;
I've put the name of the class I'm doing the find on as the first parameter, the include field I used as the 2nd.   It works great with rails 1.2!
&lt;/p&gt;
&lt;!-- technorati tags start --&gt;&lt;p style="text-align:right;font-size:10px;"&gt;Technorati Tags: &lt;a href="http://www.technorati.com/tag/ruby" rel="tag"&gt;ruby&lt;/a&gt;, &lt;a href="http://www.technorati.com/tag/rails" rel="tag"&gt;rails&lt;/a&gt;, &lt;a href="http://www.technorati.com/tag/ruby on rails" rel="tag"&gt;ruby on rails&lt;/a&gt;&lt;/p&gt;&lt;!-- technorati tags end --&gt;</description>
      <pubDate>Fri, 13 Apr 2007 15:51:06 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:804dc6ff-7504-49ea-ac30-3808292e4330</guid>
      <author>kevin</author>
      <link>http://kevinwatt.org/articles/2007/04/13/rails-how-to-eager-load-associations-with-select-only-load-select-columns-from-each-table</link>
      <category>Coding</category>
    </item>
    <item>
      <title>"Rails: How to eager load associations with :select (only load select columns from each table)" by Avi</title>
      <description>Dude, sickness.</description>
      <pubDate>Mon, 17 Sep 2007 22:03:16 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:1b84cacb-921e-4e3e-aa6b-c315d8311a21</guid>
      <link>http://kevinwatt.org/articles/2007/04/13/rails-how-to-eager-load-associations-with-select-only-load-select-columns-from-each-table#comment-46681</link>
    </item>
    <item>
      <title>"Rails: How to eager load associations with :select (only load select columns from each table)" by Kevin</title>
      <description>Really works great with rails 1.2 (thanks for this comment). Wish you good luck in your future projects!</description>
      <pubDate>Fri, 13 Jul 2007 04:07:20 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:2cff9087-ff2e-4a04-a463-c406533fbf47</guid>
      <link>http://kevinwatt.org/articles/2007/04/13/rails-how-to-eager-load-associations-with-select-only-load-select-columns-from-each-table#comment-43275</link>
    </item>
    <item>
      <title>"Rails: How to eager load associations with :select (only load select columns from each table)" by Michael K.</title>
      <description>you know, your head works in the right way... my worked in the opposite way... I'll take your idea ;)</description>
      <pubDate>Wed, 04 Jul 2007 04:49:04 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:69335ceb-7903-45bf-aaea-6988a0559404</guid>
      <link>http://kevinwatt.org/articles/2007/04/13/rails-how-to-eager-load-associations-with-select-only-load-select-columns-from-each-table#comment-42635</link>
    </item>
    <item>
      <title>"Rails: How to eager load associations with :select (only load select columns from each table)" by Tim</title>
      <description>really works! Thanks for explanations :)</description>
      <pubDate>Fri, 18 May 2007 08:20:13 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:967fcd20-80ff-48bd-9024-6602db9d1a84</guid>
      <link>http://kevinwatt.org/articles/2007/04/13/rails-how-to-eager-load-associations-with-select-only-load-select-columns-from-each-table#comment-34953</link>
    </item>
  </channel>
</rss>
