Check joins when determining preloading of includes
Reported by Peter Wagenet | July 1st, 2008 @ 07:08 PM | in 2.x
Currently when calling ActiveRecord::Base#find with both an include clause and an order clause which references the include, ActiveRecord::Associations::ClassMethods#find_with_associations is used. This discards the select clause. In some cases, this is not the intended behavior. Take the following contrived example:
Person.find(:all, :select => "people.*, email.address IS NOT NULL AS has_email", :order => "email.address", :include => :email, :phone, :address)
The generated query will end up looking something like:
SELECT `people`.`id` AS t0_r0, `people`.`first_name` AS t0_r1 ... FROM `people` LEFT OUTER JOIN `emails` ON `people`.id = `emails`.person_id LEFT OUTER JOIN `phones` ON `people`.id = `phones`.person_id LEFT OUTER JOIN `addresses` ON `people`.id = `addresses`.person_id ORDER BY email.address;
As you can see, the provided select clause has been discarded in place of the complex one needed to load the includes.
I suggest that the user should be able to also specify a join clause which, if it includes the right tables, will allow ActiveRecord::AssociationPreload::ClassMethods#preload_associations to be used instead.
This has a two fold benefit of allowing a select clause to be used. And of still splitting the eager loading into multiple queries.
A revised query would look something like:
Person.find(:all, :select => "people.*, email.address IS NOT NULL AS has_email", :order => "email.address", :joins => :email, :include => :email, :phone, :address)
With the first query (there will now be multiple for eager loading), looking like:
SELECT people.*, email.address IS NOT NULL AS has_email FROM `people` INNER JOIN `emails` ON `people`.id = `emails`.person_id ORDER BY email.address;
A patch for this functionality is attached.
Comments and changes to this ticket
-
-

Tiago Macedo October 23rd, 2008 @ 04:27 PM
I found a bug in your code (it isn't specific to YOUR code, but it wasn't there before):
If you add this to your test:
posts = Post.find(:all, :select => "posts.*, authors.name AS test_name", :include => [ :comments, :author, :categories ], :limit => 1) assert_equal "David", posts.first.test_nameYou'll see that it fails. This will only happen using the old behaviour and a limit clause. This wasn't an issue before because the select clause was being discarded.
I think you'll have to check which tables are referenced in the select clause and force their eager load (in the same way that's being done for the conditions and order options)
-
Peter Wagenet July 1st, 2008 @ 11:44 PM
Turns out the real issue isn't the order or limit clause. It's the select clause. If the any of the select, conditions or order reference the include tables then the select clause ends up getting discarded. With my fix, the solution in any of these events is to use an additional joins parameter as shown above. I've changed my tests to be more generic.
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
The Git repository resides at http://github.com/rails
Check out the current development trunk (Edge Rails) with:
git clone git://github.com/rails/rails.git
Creating or reviewing a patch
See the contributor guide.
Creating a feature request
Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".
