我刚刚将一个rails应用程序从SQLLite迁移到MS Sql Server。我有一个组在SqlLite下工作正常但在使用SQL / TinyTds时出现如下错误。
ActiveRecord::StatementInvalid in Complaints#report_by_product
Showing C:/Users/cmendla/RubymineProjects/internal_complaints/app/views/complaints/report_by_product.html.erb where line #10 raised:
TinyTds::Error: Column 'ic.complaints.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.: EXEC sp_executesql N'SELECT [ic].[complaints].* FROM [ic].[complaints] GROUP BY [ic].[complaints].[product_name]'
Rails.root: C:/Users/cmendla/RubymineProjects/internal_complaints
Application Trace | Framework Trace | Full Trace
app/views/complaints/report_by_product.html.erb:10:in `_app_views_complaints_report_by_product_html_erb___717152080_78192168'
app/controllers/complaints_controller.rb:18:in `report_by_product'
投诉控制器中的副产品def
def report_by_product
@complaints = Complaint.all
@complaints_group = Complaint.group("product_name") # causes the report to be grouped by the product
respond_to do |format|
format.html
format.json { render json: @complaints }
end
端
副产品视图的一部分是
<tr>
<% @complaints_group.each do |complaint_group| %>
<% $complaint_group_name = complaint_group.product_name %>
<td>
<%= complaint_group.product_name %>
</td>
<td style="text-align: right;">
<%= (Complaint.where("product_name like ?", $complaint_group_name )).count %>
</td>
<td style="text-align: right;" >
<%= (Complaint.where("product_name like ?
AND complaints.created_at > ?",
$complaint_group_name, Date.today-360).count) %>
</td>
<td style="text-align: right;">
<%= (Complaint.where("product_name like ?
AND complaints.created_at > ?
AND complaints.created_at < ? ",
$complaint_group_name,
Date.today-90,
Date.today).count) %>
我的TDS宝石
Installing activerecord-sqlserver-adapter 4.2.8 (was 4.2.4)
Using tiny_tds 0.7.0
答案 0 :(得分:1)
我得到了这个工作。我不得不在控制器中更改分组语句。
从:
parent
致:
parentA
这让它运作正常。