Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8728067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:33:14+00:00 2026-06-13T08:33:14+00:00

In certain cases when I get an ActiveRecord Relation I’m experiencing strange behavior with

  • 0

In certain cases when I get an ActiveRecord Relation I’m experiencing strange behavior with .each on an ActiveRecord::Relation

It seems to be when ActiveRecord::Relation delegates :each to :to => :to_a (source)

@tasks = Task.find_task(list, {:week_id => 1})

Basically, there’s a lengthy class method that takes an object (list, and a hash with a :week_id)

A bunch of filtering & queries happen within this find_task method, but it ends up returning a relation to @tasks

Then, in the template, I have:

<% @tasks.each do |task| %>
.
.
.
<% end %>

For whatever reason, no matter the size of @tasks, it takes ~3 minutes. I Can replicate this same behavior by calling @tasks.to_a Even if @tasks, an instance of ActiveRecord::Relation is only two records, calling to_a on them takes > 3 minutes.

It doesn’t happen on all :week_ids, only on a specific week_id, for example: :week_id => 1

The SQL executes fine and I get a relation back, it just seems to be a problem with enumerable on a specific ActiveRecord::Relation.

Update

Inside the algorithm (I think this means the class method) I do a LOT of eager loading. So Postgres does a lot of LEFT OUTER JOINs and I’ve indexed all the tables that this needs to happen on.

An explain analyze shows that all scans are index scans 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.

Update 2
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 top:

 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

When it finally finishes, The server shows this;

  • eager loading: 200 ms, 139 ms then
  • a big SQL Query with lots of LEFT OUTER JOINS in 33,000 ms, (long, but not where the majority is) then
  • 257,000 ms in the template.

And when I replicate the behavior in the template I see that it takes ~3-4 minutes to call to_a on the @tasks Relation.

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 top I can only see the ruby process running?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:33:16+00:00Added an answer on June 13, 2026 at 8:33 am

    The problem was with the eager loading. When calling an enumerable method on an ActiveRecord::Relation instance it gets delegated to .to_a, which can take a really long time with a huge set of relations. Even though I was looping through @tasks, I had eager loaded so many objects that .to_a was taking too long.

    My short-term fix is to simply eager load fewer objects, even though it ends up hurting me with n+1 queries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In certain cases - especially when an exception escapes a destructor during stack unwinding
On certain error cases ATL invokes AtlThrow() which is implemented as ATL::AtlThrowImpl() which in
My app has several UIViewControllers in a stack (under a UINavigationController). In certain cases
I'm using ajax calls to retrieve some HTML data. In certain cases, there are
I need to get the value of the content attribute of a certain meta
I am still trying to wrap my head around certain cases in REST where
I have an application X that uses another application Y in certain cases. X
I'm using a scripting functoid in a BizTalk map. In a certain case, I
Case i'm trying to write a GWTTestCase for a certain class, used as a
I have a use case where I only need to store certain fields to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.