急切加载时狂欢活动记录对象为零

时间:2017-11-16 09:45:28

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord spree

我正在尝试查询项目并循环遍历它们。 li不是nil,但访问product会导致NilClass错误。

Spree::LineItem.includes(:product).where(order_id: oids).find_each do |li|
  unless li.nil?
    unless li.product.nil?
      puts li.product.id
    end
  end
end

这会在行号3

上引发错误
NoMethodError: undefined method `product' for nil:NilClass
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/spree_core-3.0.0/app/models/spree/line_item.rb:95:in `product'
from (irb):5:in `block in irb_binding'
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/activerecord-4.2.8/lib/active_record/relation/batches.rb:51:in `block (2 levels) in find_each'
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/activerecord-4.2.8/lib/active_record/relation/batches.rb:51:in `each'
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/activerecord-4.2.8/lib/active_record/relation/batches.rb:51:in `block in find_each'
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/activerecord-4.2.8/lib/active_record/relation/batches.rb:124:in `find_in_batches'
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/activerecord-4.2.8/lib/active_record/relation/batches.rb:50:in `find_each'
from (irb):3
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands/console.rb:110:in `start'
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands/console.rb:9:in `start'
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/vagrant/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:9:in `require'
from bin/rails:9:in `<main>'

正如我们所见,我们已经检查过li不是零。我不确定这是一个错误还是我做错了什么。

以前有人见过这样的事吗?

0 个答案:

没有答案