Ruby on rails nginx 502请求错误,因为rails查询数据库
我的应用程序上有一个页面,它从数据库中加载一组数据,以便显示一些图像 我从ngnx接收到一个502坏网关,因为所有处理都花费了太长时间,到达rails超时 这是我的代码:Ruby on rails nginx 502请求错误,因为rails查询数据库,ruby-on-rails,ruby,nginx,Ruby On Rails,Ruby,Nginx,我的应用程序上有一个页面,它从数据库中加载一组数据,以便显示一些图像 我从ngnx接收到一个502坏网关,因为所有处理都花费了太长时间,到达rails超时 这是我的代码: <% puts @pictures.count %> <% @pictures.each do |picture| %> <% puts picture.id %> <div class="page-header"> <div class="jumbotro
<% puts @pictures.count %>
<% @pictures.each do |picture| %>
<% puts picture.id %>
<div class="page-header">
<div class="jumbotron">
<div class="container">
<div class="row">
<h1 class="title_post" style="color:#015836"><%= picture.title %></h1>
<p style="font-size:12px; color:#59AA41"><%= picture.created_at.strftime("%d/%m/%Y %H:%M") %></p>
<p><%= picture.description.truncate(180) %></p>
<% picture.pictureDatum.each do |image| %>
<% puts image.id %>
<div class="col-md-3">
<%= image_tag image.image.url, class: "col-xs-3 col-md-3 img-rounded img-responsive thumbnail" %>
</div>
<% end %>
<p class="bnt_ver_mais">
<%= link_to 'LER MAIS', picture, class:"btn btn-info" %>
<%= link_to 'DELETE', { action: :destroy, id: picture.id }, class:"btn btn-danger bnt_voltar_post", method: :delete, data: { confirm: 'Are you sure?' } %>
</p>
处理所有内容花费的时间太长,因此rails达到其超时并返回到客户端502坏网关
Ruby应该处理14个“行”,其中有3个图像与之关联,所以总共42个
我想知道如何处理这些数据而不达到超时
编辑
运行一些测试后
我执行
Benchmark.measure{Picture.all.each{p|p.picturedatam.all.to_a}.real
总时间仅为=>5.45448455299929
现在我迷路了!我真的不知道是什么原因导致rails花了这么长时间来处理这个页面。让你的数据库更快?更快的查询?增加nginx中的超时时间?您确切地知道为什么生成几个链接需要如此长的时间吗?如果是,请修复该问题。否则,请先找出它。
picturedatam
的定义是什么?那是一个协会吗?还是一种方法?如果您的数据集很大,那么您的问题可能是最大的,但是如果您的数据像您所说的那么小,那么这不应该是一个问题。要测试这一点,打开rails控制台并运行:require'benchmark';Benchmark.measure{Picture.all.each{p|p.picturedatam.all.to_a}}.real
。它应该返回在所有图片的PicturedAtom上迭代的秒数。哇!看起来只需要5秒钟。。。现在我不知道是什么让rails花了这么长时间来处理。。。