Column definitions not cached for calculations on has_many association with :include

Hi everyone,

While trying out Rails 3 for one of my applications, I have been bitten by the following.

When doing a calculation such as .count on has_many associations that use :include => some_model, it appears column definitions are not cached. It's not cached when config.cache_classes = true, or even in the same request. This causes pretty serious performance issues, because retrieving column definitions this often is expensive.

A short example:

class Article < ActiveRecord::Base
  has_many :comments, :include => :author
end

class Comment < ActiveRecord::Base
  belongs_to :author
end

class Author < ActiveRecord::Base
end

Article.last.comments.count # Retrieves column definitions from db every single time.

Attached to this e-mail is a patch with a failing test. I don't know if there are other kinds of queries that suffer from the same problem.

I would also be happy to write a patch that fixes the problem, but so far I'm having trouble understanding where the responsibility of caching the column definitions should be exactly (or even figuring out where the column definitions are cached right now). Any help or suggestions on how to tackle this problem are most welcome.

Thank you!

Kind regards,

Rolf Timmermans

0001-Added-failing-test-for-caching-of-column-information.patch (1.05 KB)

Attached to this e-mail is a patch with a failing test. I don't know if there are other kinds of queries that suffer from the same problem.

Could you open a lighthouse ticket with this please? We'll need it in
there to make sure we don't miss it for 3.0 final.

Sure, a ticket has been created (https://rails.lighthouseapp.com/projects/8994/tickets/4991).

In the mean time I also found out that the same problem occurs when using joins(), e.g. Articles.joins(:comments).all. This time it happens even with has_many associations without :include.

Kind regards,

Rolf Timmermans