Ruby on rails 急加载时Spree活动记录对象为零
我正在尝试查询该项并遍历它们。Ruby on rails 急加载时Spree活动记录对象为零,ruby-on-rails,ruby-on-rails-4,rails-activerecord,spree,Ruby On Rails,Ruby On Rails 4,Rails Activerecord,Spree,我正在尝试查询该项并遍历它们。li不是nil,但访问其上的product会抛出nil类错误 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 `p
li
不是nil,但访问其上的product
会抛出nil类错误
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>'
NoMethodError:nil:NilClass的未定义方法“product”
from/home/vagrant/.rvm/gems/ruby-2.3.3/gems/spree_core-3.0.0/app/models/spree/line_item.rb:95:在“产品”中
from(irb):5:in`block in irb_binding'
from/home/vagrant/.rvm/gems/ruby-2.3.3/gems/activerecord-4.2.8/lib/active\u record/relation/batches.rb:51:find\u中的“块(两级)”中
from/home/vagrant/.rvm/gems/ruby-2.3.3/gems/activerecord-4.2.8/lib/active\u record/relation/batches.rb:51:in'each'
from/home/vagrant/.rvm/gems/ruby-2.3.3/gems/activerecord-4.2.8/lib/active\u record/relation/batches.rb:51:位于“查找中的块”中
from/home/vagrant/.rvm/gems/ruby-2.3.3/gems/activerecord-4.2.8/lib/active\u record/relation/batches.rb:124:“在批中查找”
from/home/vagrant/.rvm/gems/ruby-2.3.3/gems/activerecord-4.2.8/lib/active\u record/relation/batches.rb:50:in“find\u each”
来自(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\u tasks.rb:68:在“控制台”中
from/home/vagrant/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands/commands\u tasks.rb:39:in“run\u command!”
from/home/vagrant/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands.rb:17:in`'
从箱子/轨道:9:in“require”
从箱子/轨道:9:in`'
正如我们所看到的,我们已经检查了li
不是nil。我不确定这是一个bug还是我做错了什么
以前有人见过这样的东西吗?full stacktrace请注意,我认为错误来自另一个位置+1,您的代码正常,不应该有任何错误。请将完整的行项目型号代码、完整的方法代码粘贴到您调用此部件的位置,并粘贴完整的StackTrace不确定这样使用的每个是否正确。你试过使用
每种吗?是的,我试过了。:)不起作用。我会尽快将要求的详细信息添加到问题中。@HieuPham我认为你是正确的,错误来自其他地方。我已经更新了这个问题。请完整stacktrace,我认为错误来自另一个地方+1,您的代码是正确的,不应该有任何错误。请将完整的行项目型号代码、完整的方法代码粘贴到您调用此部件的位置,并粘贴完整的StackTrace不确定这样使用的每个
是否正确。你试过使用每种吗?是的,我试过了。:)不起作用。我会尽快将要求的详细信息添加到问题中。@HieuPham我认为你是正确的,错误来自其他地方。我已经更新了这个问题。