I am trying to perform group_by operation on my rails model,but there seem to be some problem. I have a model named student as
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 console when I run
s = Student.all.group("date(created_at)")
I have the following error:
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)
However when I run
s = Student.all.group("date(created_at)").count
it runs successfully.
I am a new to rails.
Rails 4.2.6 Ruby 2.3.1