【问题标题】:Deploying rails app on heroku, not displaying SQL query在heroku上部署rails应用程序,不显示SQL查询
【发布时间】:2016-03-20 16:39:32
【问题描述】:

你能帮我解决这个问题吗?

我无法在生产版本(部署在 Heroku)上执行“ActiveRecord::Base.connection.execute("SQL")”,尽管它可以在本地运行。

<table class="table table-striped">
<thead>
<tr>
  <th>#</th>
  <th>Name</th>
  <th>Games</th>
  <th>Wins</th>
  <th>Win Ratio</th>
</tr>
</thead>
<tbody>
<% ActiveRecord::Base.connection.execute("
select * from players").each_with_index do |performer, i| %>
  <tr>
    <td><%= (i+1) %> </td>
    <td><%= performer[0] %> </td>
    <td><%= performer[1] %></td>
    <td><%= performer[2] %></td>
    <th><%= performer[3] %> <%='%'%></th>
  </tr>
  <% end %>
  </tbody>

发展 生产 HEROKU 原木

【问题讨论】:

  • 生产中的players表有记录吗?
  • 是的,有记录。所有其他数据也显示在生产版本上。只是缺少这个 SQL 查询执行。
  • 好的。服务器日志告诉你什么?你可以在问题中发布它吗?
  • 我在问题描述中添加了“heroku 日志”。
  • 你也在开发中使用 Postgres 吗?您的数据库中是否有包含名称、游戏、胜率和胜率字段的玩家表?

标签: ruby-on-rails postgresql heroku database-connection


【解决方案1】:

这应该是评论(抱歉还没有代表)

既然你要召唤所有玩家,为什么不做类似的事情

<% @players.each do |p| %>
<td><%= (i+1) %> </td> 
<td><%= p.name %> </td>
<td><%= p.games %></td> 
<td><%= p.wins %></td> 
<% percent = p.wins / p.games %>
<% percent = percent * 100 %>
<% percent = number_with_precision(percent, :precision => 0) %>
<th><%= percent %> %</th>
<% end %>

在控制器中执行@players = Player.all 之类的操作

这将消除您视图中对任何 SQL 语句的需求,消除了拥有 pg 和 sql 的问题

【讨论】:

    猜你喜欢
    • 2014-08-08
    • 2016-06-23
    • 2020-05-15
    • 2012-06-28
    • 2015-03-04
    • 2013-01-08
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    相关资源
    最近更新 更多