Ruby on rails Activerecord集合迭代:如果未读取字段,则出错

Ruby on rails Activerecord集合迭代:如果未读取字段,则出错,ruby-on-rails,activerecord,iterator,Ruby On Rails,Activerecord,Iterator,我尝试迭代AR模型集合时有奇怪的行为: @created = QueueOnline.new Order.online.created.to_a.each do |o| # p o.customer @created.try_to_offer_translators(o) end 其中: class OrderQueue def try_to_offer_translators(order) if order.sticky_translator? proces

我尝试迭代AR模型集合时有奇怪的行为:

@created = QueueOnline.new
Order.online.created.to_a.each do |o|

  # p o.customer
  @created.try_to_offer_translators(o)
end
其中:

class OrderQueue
  def try_to_offer_translators(order)
    if order.sticky_translator?
      process_sticky_translator(order)
    return
  end
  #byebug #if I stop this and get order.customer - all work fine
  translators_for_order(order).each do |translator|
    offer_to(order, translator)
  end
end
如果我不停止或从
order.customer
获取值,它将是
nil
,但如果我停止或执行简单的调用
p order.customer
底层函数将正常工作

附言:如果我接受单个项目
订单
,将其放入数组中-它也可以正常工作

[order].each do |o|
  @created.try_to_offer_translators(o)
end
================

更新: 我明白我的问题是因为没有加载关系(客户)。 如果我改为:

Order.eager_load(:customer).online.created

它会很好用的。但我一直认为,如果对象a没有加载,它将在必要时加载。

是否将
联机
创建的
视为过滤器?请出示您的
订单
型号好吗?为什么您发现需要一个
?来自筛选器的记录集合将响应
每个方法。@SteveTurczyn,是的,它是筛选器(作用域)。我使用
测试ActiveRecord是否有问题。你不应该从ActiveRecord得到问题。如果键入
Order.online.created.class
,您会看到什么类?它应该是ActiveRecord关系。问题可能在STI中:order有两个关系-customer和translator,但两者都是用户并使用一个表。