我想對我的Rails模型執行group_by操作,但似乎存在一些問題。我有一個名爲學生模型無法在Rails活動記錄中執行組操作
class CreateStudents < ActiveRecord::Migration
def change
create_table :students do |t|
t.integer :student_id
t.string :department
t.integer :maths
t.integer :physics
t.integer :chemistry
t.string :year
t.timestamps null: false
end
end
end
on Rails的控制檯,當我運行
s = Student.all.group("date(created_at)")
我有以下錯誤:
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR: column "students.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "students".* FROM "students" GROUP BY date(created_at...
^
: SELECT "students".* FROM "students" GROUP BY date(created_at)
然而,當我運行
s = Student.all.group("date(created_at)").count
它成功運行。
我是一個新的鐵路。
的Rails 4.2.6 的Ruby 2.3.1
可能重複的[鏈接](http://stackoverflow.com/questions/18061285/postgresql-must-appear-in-the-group-by-clause-or-be -used-an-aggregate-functi) –