我正在将 Rails 3 应用程序迁移到 Rails 6,并使用 activerecord-reputation-system gem 来计算选票。 应用在迁移之前运行没有问题。
当我在本地主机上运行该应用程序时,用户注册等工作正常,但是当我进入应用程序的"建议"部分时,出现以下错误:
活动记录::语句在建议中无效#索引 PG::语法错误:错误:输入末尾的语法错误 第 1 行:...ation_name = "投票"和 rs_reputations.active = TRUE 其中
推荐模型:
class Recommendation < ActiveRecord::Base
has_reputation :votes, source: :user, aggregated_by: :sum
belongs_to :user
end
建议控制器:
class RecommendationsController < ApplicationController
before_action :set_recommendation, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
@category = ["product", "marketing", "business", "team", "general"]
@recommendations = Recommendation.find_with_reputation(:votes, :all)
@product = Recommendation.where(category: "Product").order("votes DESC")
@marketing = Recommendation.where(category: "Marketing").order("votes DESC")
@business = Recommendation.where(category: "Business").order("votes DESC")
@team = Recommendation.where(category: "Team/culture").order("votes DESC")
@general = Recommendation.where(category: "General").order("votes DESC")
end
和建议索引视图:
<h3>Recommendations</h3>
<h5>(<%= link_to new_recommendation_path, "data-no-turbolink" => true do %>
<span class="glyphicon glyphicon-plus"></span> New Recommendation
<% end %>)</h5>
<table class="table table-striped">
<tr>
<th>Submitted</th>
<th>Category</th>
<th>Title</th>
<th>Description</th>
<th>Votes</th>
</tr>
<% @recommendations.each do |recommendation| %>
<tr>
<td><%= recommendation.created_at.strftime("%B %d, %Y") %><br>
<small>    by: <strong><%= recommendation.user.name %></strong></td>
<td><%= recommendation.category %></td>
<td><%= recommendation.title %></td>
<td><%= recommendation.description %></td>
<td>
<% if current_user && recommendation.has_evaluation?(:votes, current_user) %>
<%= link_to downvote_recommendation_path(recommendation), method: "post" do %>
<span class="glyphicon glyphicon-heart"></span>
<% end %>
<% else %>
<%= link_to vote_recommendation_path(recommendation, type: "up"), method: "post" do %>
<span class="glyphicon glyphicon-heart-empty"></span>
<% end %>
<% end %>
(<%= recommendation.reputation_for(:votes).to_i %>)
</td>
</tr>
<% end %>
</table>
错误被抛出到:
<% @recommendations.each do |recommendation| %>
从终端我看到:
Started GET "/recommendations" for ::1 at 2019-12-29 10:06:57 -0800
(3.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by RecommendationsController#index as HTML
User Load (6.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendering recommendations/index.html.erb within layouts/application
Recommendation Load (0.5ms) SELECT recommendations.*, COALESCE(rs_reputations.value, 0) AS votes FROM "recommendations" LEFT JOIN rs_reputations ON recommendations.id = rs_reputations.target_id AND rs_reputations.target_type = 'Recommendation' AND rs_reputations.reputation_name = 'votes' AND rs_reputations.active = TRUE WHERE
Rendered recommendations/index.html.erb within layouts/application (Duration: 3.0ms | Allocations: 1597)
Completed 500 Internal Server Error in 49ms (ActiveRecord: 17.2ms | Allocations: 21303)
ActionView::Template::Error (PG::SyntaxError: ERROR: syntax error at end of input
LINE 1: ...ation_name = 'votes' AND rs_reputations.active = TRUE WHERE
能够通过更改控制器来解决:
@recommendations = Recommendation.find_with_reputation(:votes, :all)
自
@recommendations = Recommendation.with_reputation(:votes)
将find_with_reputation更改为with_reputation解决了它