.collect在belongs_to上提出NoMethodError:未定义的方法`add'代表nil:NilClass

时间:2010-08-25 01:44:03

标签: ruby-on-rails ruby ruby-on-rails-3

设置:Rails 3 RC2,Ruby 1.9.2 p0(也在Rails 3 beta 4上试用,Ruby 1.8.7 p174)

我有一个非常基本的购物车设置:

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

创建Products和OrderItems后,我进入控制台并尝试以下操作:

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

我收到错误:

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>'

我知道产品确实存在并且order_item有效...如果我执行以下操作:

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

然后我得到了我的期望:=> [{:name=>3, :quantity=>1}, {:name=>1, :quantity=>4}]

我不确定为什么在关联对象(item.product.name)上调用属性会引发错误 - 有人可以为我解释这个吗?

修改 我刚刚添加以下内容作为评论,但格式化很糟糕......

我也忘了提到这只发生在1个order_items以上。 1个订单商品按预期工作。以下似乎解决了我的问题...

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

由于

3 个答案:

答案 0 :(得分:0)

这至少是错误的(或者你输错了)

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

很难说它是否在抱怨所有这些方法缺少飞来飞去但至少要纠正。欢呼声。

答案 1 :(得分:0)

似乎子弹宝石(目前在RC中)造成了这个问题。我在bullet github页面上发现了这个问题( http://github.com/flyerhzm/bullet/issues/#issue/31)。我已将信息留在那里供业主查看。

答案 2 :(得分:0)

抱歉延迟,我想如果你将bullet gem升级到2.0.0.rc2就应该解决这个问题