This project is archived and is in readonly mode.

#363 ✓duplicate
Dimitrij Denissenko

Association pre-loading fails with has_and_belongs_to_many (2.1.0)

Reported by Dimitrij Denissenko | June 7th, 2008 @ 04:36 PM

Association pre-loading fails with has_and_belongs_to_many if additional includes are defined.

class User < ActiveRecord::Base
  has_and_belongs_to_many :groups, :include => [:permissions] # works if :include => [:permissions] is NOT specified
end
class Group < ActiveRecord::Base
  has_and_belongs_to_many :permissions
end
class Permission < ActiveRecord::Base
end
User.find 1, :include => [:groups]

NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/association_preload.rb:45:in `add_preloaded_records_to_collection'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/association_preload.rb:63:in `set_association_collection_records'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/association_preload.rb:61:in `each'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/association_preload.rb:61:in `set_association_collection_records'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/association_preload.rb:109:in `preload_has_and_belongs_to_many_association'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/association_preload.rb:40:in `send'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/association_preload.rb:40:in `preload_one_association'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/association_preload.rb:38:in `each'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/association_preload.rb:38:in `preload_one_association'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/association_preload.rb:17:in `preload_associations'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/association_preload.rb:16:in `preload_associations'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/association_preload.rb:16:in `each'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/association_preload.rb:16:in `preload_associations'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1343:in `find_every'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1376:in `find_one'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1362:in `find_from_ids'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:537:in `find'
	from (irb):8

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages