Eager fetching a HABTM fails when the HABTM uses the "include" option
Reported by Mark Roghelia | June 2nd, 2008 @ 10:53 PM
This ticket assumes that the fix for eager fetching HABTMs that is detailed in ticket #304 has been applied.
Eager fetching a "has_and_belongs_to_many" association as part of a find generates as error when that association is defined to include another HABTM with the "include" option.
Consider the following example. A UserProfile class has a "has_and_belongs_to_many" association with UserRole. UserRole in turn has a HABTM with Permission. In UserProfile, the "user_roles" association is defined to include the permissions, as follows:
class UserProfile < ActiveRecord::Base
has_and_belongs_to_many :user_roles, :include => :permissions
end
If I call UserProfile.find(1, :include => :user_roles), I get the following error:
/Library/Ruby/Gems/1.8/gems/rails-2.1.0/lib/commands/runner.rb:47: /Users/mark/src/msd/lib/preload_association.rb:45:in `add_preloaded_records_to_collection': You have a nil object when you didn't expect it! (NoMethodError)
You might have expected an instance of Array.
The error occurred while evaluating nil.each from /Users/mark/src/msd/lib/preload_association.rb:63:in `set_association_collection_records'
from /Users/mark/src/msd/lib/preload_association.rb:61:in `each'
from /Users/mark/src/msd/lib/preload_association.rb:61:in `set_association_collection_records'
from /Users/mark/src/msd/lib/preload_association.rb:109:in `preload_has_and_belongs_to_many_association'
from /Users/mark/src/msd/lib/preload_association.rb:40:in `send'
from /Users/mark/src/msd/lib/preload_association.rb:40:in `preload_one_association'
from /Users/mark/src/msd/lib/preload_association.rb:38:in `each'
from /Users/mark/src/msd/lib/preload_association.rb:38:in `preload_one_association'
... 11 levels...
from /Library/Ruby/Gems/1.8/gems/rails-2.1.0/lib/commands/runner.rb:47
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `require'
from ./script/runner:3
This seems to result from the "_parent_id" column not being included in the query. If I leave ":include => :permission" out of the definition of "user_roles", this works (assuming the patch from ticket #304 has been applied).
Comments and changes to this ticket
-

James Kebinger June 5th, 2008 @ 10:47 PM
I just hit this problem myself on 2.1 w/ postgres.
I can concur the _parent_id is left out, as it looks like the :select option (which contains the needed _parent_id) is completely ignored in
ActiveRecord::Associations:construct_finder_sql_with_included_associations
-

James Kebinger October 23rd, 2008 @ 04:23 PM
I suspect this problem is linked with #77 http://rails.lighthouseapp.com/p...
-
Pratik July 23rd, 2008 @ 02:12 PM
- → Tag changed from to 2.1 activerecord bug eager_loading
- → State changed from new to duplicate
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
Repository is at http://github.com/rails/rails
Check out the development master (Edge Rails):
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"..
