This project is archived and is in readonly mode.

#4662 new
dalyons

Habtm collection_singular_ids bug with :include

Reported by dalyons | May 21st, 2010 @ 02:41 AM

This is my first ticket, so be gentle :)

Summary: when I use :include on a has_and_belongs_to_many association, it causes collection_singular_ids to fail with ActiveRecord::MissingAttributeError.

Rails v2.3.5

Code:

classes

class Product < ActiveRecord::Base
  has_and_belongs_to_many :vendors, :include => :state
end

class Vendor < ActiveRecord::Base
  has_and_belongs_to_many :products
  belongs_to :state
end

class State < ActiveRecord::Base
  has_many :vendors
end

Failing code:

v = Vendor.create
p = Product.create
p.vendors << v
p.vendor_ids  #works as expected, returns [v.id]

Product.find(p.id).vendor_ids         #re-fetch the product and try again
**ActiveRecord::MissingAttributeError: missing attribute: state_id**

p = Product.find(p.id)
p.vendors               #force an association load
p.vendor_ids            #works as expected, returns [v.id]

backtrace

>>Product.find(p.id).vendor_ids
ActiveRecord::MissingAttributeError: missing attribute: state_id
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/association_preload.rb:309:in `send'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/association_preload.rb:309:in `preload_belongs_to_association'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/association_preload.rb:308:in `each'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/association_preload.rb:308:in `preload_belongs_to_association'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/association_preload.rb:120:in `send'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/association_preload.rb:120:in `preload_one_association'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/ordered_hash.rb:97:in `each'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/ordered_hash.rb:97:in `each'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/association_preload.rb:114:in `preload_one_association'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/association_preload.rb:91:in `preload_associations'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/association_preload.rb:90:in `preload_associations'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/association_preload.rb:90:in `each'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/association_preload.rb:90:in `preload_associations'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1550:in `find_every'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:615:in `find'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:635:in `all'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:380:in `send'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:380:in `method_missing'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2143:in `with_scope'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:206:in `send'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:206:in `with_scope'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:376:in `method_missing'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1313:in `vendor_ids'
    from (irb):14>>

It only occurs if you try and access the vendor_ids property before the vendors association collection has been loaded(eg by accessing that property)

I just tested that code with a new, clean 2.3.5 app with just the classes above, and I get the errors as described.
I can work around it by not using the :include option, but I thought id record it here as I couldn't find any info on this behaviour anywhere else on the net.

If i need to include any other info please let me know!

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>

Pages