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 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