Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道N+;1查询_Ruby On Rails - Fatal编程技术网

Ruby on rails 轨道N+;1查询

Ruby on rails 轨道N+;1查询,ruby-on-rails,Ruby On Rails,有以下Rails代码: 控制器: class OrdersController < ApplicationController def index @orders = current_hotel.restaurant_orders end end 如果我理解正确,Rails将发出1+2*N请求。我怎样才能使它更快?我应该添加“包括”吗?提前感谢我想简单地使用即时加载就可以完成这项任务。如果您想确定将执行多少查询,可以检查控制台输出。我不确定这是否会导致1+2N查询。值得一

有以下Rails代码:

控制器:

class OrdersController < ApplicationController
  def index
    @orders = current_hotel.restaurant_orders
  end
end

如果我理解正确,Rails将发出1+2*N请求。我怎样才能使它更快?我应该添加“包括”吗?提前感谢

我想简单地使用即时加载就可以完成这项任务。如果您想确定将执行多少查询,可以检查控制台输出。我不确定这是否会导致1+2N查询。值得一提的是,如果您想自动包含所有订单项目,您可以执行
当前酒店、餐厅订单。包括(:餐厅订单项目)
为什么不?1个用于获取所有订单的查询,N个用于获取每个订单的餐厅\订单\项目,N个用于获取每个订单的餐厅。如何在控制台中查看此算法?感谢当您运行rails服务器并访问网站时,SQL查询将记录在启动服务器的终端中。
json.array! @orders do |order|
  json.id order.id
  json.room order.room
  json.note order.note
  json.order_status_id order.order_status_id
  json.created_at order.created_at
  json.total_cost   order.total_cost

  json.restaurant_order_items order.restaurant_order_items do |roi|
    json.id roi.id
    json.count roi.count
    json.cost roi.cost
    json.title roi.title
    json.total_cost roi.total_cost
  end

  json.restaurant do
    json.id order.restaurant.id
    json.email order.restaurant.email
    json.phone order.restaurant.phone

    json.place { json.title order.restaurant.place.title }
  end
end