Habtm collection_singular_ids bug with :include #678

Closed
lighthouse-import opened this Issue May 16, 2011 · 2 comments

Comments

Projects
None yet
1 participant
@lighthouse-import

Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/4662
Created by dalyons - 2011-02-08 12:16:09 UTC

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!

@lighthouse-import

This comment has been minimized.

Show comment Hide comment
@lighthouse-import

lighthouse-import May 16, 2011

Imported from Lighthouse.
Comment by Craig S Walker - 2010-11-26 21:13:04 UTC

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

Imported from Lighthouse.
Comment by Craig S Walker - 2010-11-26 21:13:04 UTC

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

@lighthouse-import

This comment has been minimized.

Show comment Hide comment
@lighthouse-import

lighthouse-import May 16, 2011

Imported from Lighthouse.
Comment by Brian Racer - 2011-05-07 20:40:22 UTC

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

Imported from Lighthouse.
Comment by Brian Racer - 2011-05-07 20:40:22 UTC

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment