views:

29

answers:

3

Setup: Rails 3 RC2, Ruby 1.9.2 p0 (also tried on Rails 3 beta 4, Ruby 1.8.7 p174)

I have quite a basic shopping cart setup:

Order has_many :order_items
Order has_many :products, :through => :order_items, :dependent => :restrict

Product has_many :order_items
Product has_many :orders, :through => :order_items, :dependent => :restrict

OrderItem belongs_to :product
OrderItem belongs_to :order

After creating Products and OrderItems I head into the console and try the following:

Order.last.order_items.collect { |item| { :name => item.product.name, :quantity => item.quantity } }

And I get an error:

NoMethodError: undefined method `add' for nil:NilClass
    from /Users/tcoop/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0.rc2/lib/active_support/whiny_nil.rb:48:in `method_missing'
    from /Users/tcoop/.rvm/gems/ruby-1.9.2-p0/gems/bullet-2.0.0.rc1/lib/bullet/detector/n_plus_one_query.rb:21:in `create_notification'
    from /Users/tcoop/.rvm/gems/ruby-1.9.2-p0/gems/bullet-2.0.0.rc1/lib/bullet/detector/n_plus_one_query.rb:14:in `call_association'
    from /Users/tcoop/.rvm/gems/ruby-1.9.2-p0/gems/bullet-2.0.0.rc1/lib/bullet/active_record3.rb:78:in `load_target'
    from /Users/tcoop/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0.rc2/lib/active_record/associations/association_proxy.rb:118:in `reload'
    from /Users/tcoop/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0.rc2/lib/active_record/associations.rb:1451:in `block in association_accessor_methods'
    from (irb):6:in `block in irb_binding'
    from /Users/tcoop/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0.rc2/lib/active_record/associations/association_collection.rb:430:in `block in method_missing'
    from /Users/tcoop/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0.rc2/lib/active_record/associations/association_proxy.rb:216:in `block in method_missing'
    from /Users/tcoop/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0.rc2/lib/active_record/associations/association_proxy.rb:216:in `collect'
    from /Users/tcoop/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0.rc2/lib/active_record/associations/association_proxy.rb:216:in `method_missing'
    from /Users/tcoop/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0.rc2/lib/active_record/associations/association_collection.rb:430:in `method_missing'
    from (irb):6
    from /Users/tcoop/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0.rc2/lib/rails/commands/console.rb:44:in `start'
    from /Users/tcoop/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0.rc2/lib/rails/commands/console.rb:8:in `start'
    from /Users/tcoop/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0.rc2/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

I know the products do exist AND that the order_item is valid... If I do the following:

ruby-1.9.2-p0 > Order.last.order_items.collect { |item| { :name => item.product_id, :quantity => item.quantity } }

Then I get what I was expecting: => [{:name=>3, :quantity=>1}, {:name=>1, :quantity=>4}]

I am not sure why calling an attribute on the associated object (item.product.name) is raising errors - can anyone shed some light on this for me please?

EDIT I just added the following as a comment but the formatting is horrible...

I also forgot to mention that this only happens with there are more than 1 order_items. 1 order item works as expected. AND the following seems to solve my problem...

products = order.order_items.collect do |item|
  uid = item.product.uid
  { :ProductUID => uid, :Quantity => item.quantity }
end

Thanks

A: 

This is wrong at least (or you made a typo)

Product has_many :order, :through => :order_items, :dependent => :restrict
                       |
                       V
Product has_many :orders, :through => :order_items, :dependent => :restrict

Hard to say if it's complaining about that with all those method missing flying around but correct that at least. Cheers.

Hugo
the funny part is that this made me realize that I used the plural in a belongs_to on the app I am working on at the moment. :D
Hugo
That's a typo. I will fix it.
Tim Cooper
A: 

It seems that the bullet gem (currently in RC) was causing the problem. I found this issue (http://github.com/flyerhzm/bullet/issues/#issue/31) on the bullet github page. I have left my information there for the owner to look into.

Tim Cooper
A: 

Sorry for the delay, I guess the issue should be solved if you upgrade your bullet gem to 2.0.0.rc2

Richard Huang