1

我刚刚将一个 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
4

1 回答 1

1

我得到了这个工作。我不得不更改控制器中的分组语句。

从:

 @complaints_group = Complaint.group("product_name")         

至:

   @complaints_group = Complaint.select("product_name").group("product_name")

这让它正常工作。

于 2016-03-10T13:26:04.533 回答