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