title: Rails Active Record Relations to Enumerable
body: <p>In certain cases when I get an ActiveRecord Relation I'm experiencing strange behavior with <code>.each</code> on an ActiveRecord::Relation</p>
  
  <p>It seems to be when ActiveRecord::Relation delegates <code>:each</code> to <code>:to =&gt; :to_a</code> (<a href="https://github.com/rails/rails/blob/v3.2.6/activerecord/lib/active_record/relation/delegation.rb" rel="nofollow">source</a>)</p>
  
  <pre><code>@tasks = Task.find_task(list, {:week_id =&gt; 1})
  </code></pre>
  
  <p>Basically, there's a lengthy class method that takes an object (<code>list</code>, and a hash with a <code>:week_id</code>)</p>
  
  <p>A bunch of filtering &amp; queries happen within this <code>find_task</code> method, but it ends up returning a relation to <code>@tasks</code></p>
  
  <p>Then, in the template, I have:</p>
  
  <pre><code>&lt;% @tasks.each do |task| %&gt;
  .
  .
  .
  &lt;% end %&gt;
  </code></pre>
  
  <p>For whatever reason, no matter the size of <code>@tasks</code>, it takes ~3 minutes.  I Can replicate this same behavior by calling <code>@tasks.to_a</code>  Even if <code>@tasks</code>, an instance of ActiveRecord::Relation is only two records, calling <code>to_a</code> on them takes > 3 minutes.</p>
  
  <p>It doesn't happen on all <code>:week_id</code>s, only on a specific week_id, for example: <code>:week_id =&gt; 1</code></p>
  
  <p>The SQL executes fine and I get a relation back, it just seems to be a problem with enumerable on a specific ActiveRecord::Relation.</p>
  
  <p><em><strong>Update</em></strong></p>
  
  <p>Inside the algorithm (I think this means the class method) I do a LOT of eager loading.  So Postgres does a lot of <code>LEFT OUTER JOIN</code>s and I've indexed all the tables that this needs to happen on.</p>
  
  <p>An explain analyze shows that all scans are <code>index scans</code> and as it turns out the query executes just fine with a lot of eager loading... and I get an eager loaded 'ActiveRecord::Relation` back in a reasonable amount of time.</p>
  
  <p><em><strong>Update 2</em></strong>
  While this process is taking 3 minutes I see a postgres process run for a few seconds, and then I see this for 3 minutes as my output in <code>top</code>:</p>
  
  <pre><code> PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+    COMMAND                                                            
   8685 dylan     20   0 3407m 2.6g  904 R 99.7 69.1   1:14.49 /usr/local/bin/ruby script/rails s
  </code></pre>
  
  <p>When it finally finishes, The server shows this;</p>
  
  <ul>
  <li>eager loading: <code>200 ms</code>, <code>139 ms</code> then</li>
  <li>a big SQL Query with lots of <code>LEFT OUTER JOINS in 33,000 ms</code>, (long, but not where the majority is) then </li>
  <li><code>257,000 ms</code> in the template.</li>
  </ul>
  
  <p>And when I replicate the behavior in the template I see that it takes ~3-4 minutes to call <code>to_a</code> on the <code>@tasks</code> Relation.  </p>
  
  <p>So, when my server tells me all that time is spent in the template, and I can see calling an enumerable on the relation takes forever, is that when the query gets executed?  Even though in <code>top</code> I can only see the <code>ruby</code> process running?</p>