Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Habtm collection_singular_ids bug with :include

#4662

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!

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

State: new
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Craig S Walker
    Craig S Walker

    I've hit this error too; same stack trace, same conditions, same version of Rails.

    November 26th, 2010 @ 09:13 PM

  2. Brian Racer
    Brian Racer

    This issue does not apply to Rails 3.0.x/3.1.x

    May 7th, 2011 @ 09:40 PM