Postgresql Group_by错误

时间:2014-02-25 13:50:22

标签: ruby-on-rails ruby postgresql group-by

我正在尝试执行以下操作:

@business.placements.includes(:employee).group(:month).  order('points DESC').map(&:employee)

但是我一直收到以下错误(每当我使用group_by时因为缺乏postgresql知识而遇到问题):

ActiveRecord::StatementInvalid (PG::Error: ERROR:  column "placements.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "placements".* FROM "placements" INNER JOIN "employee...
               ^
: SELECT "placements".* FROM "placements" INNER JOIN "employees" ON "placements"."employee_id" = "employees"."id" WHERE "employees"."business_id" = 43 GROUP BY month ORDER BY points DESC)

我的架构如下(如果有任何帮助):

class Business < ActiveRecord::Base
  has_many :employees, dependent: :destroy
  has_many :placements, through: :employees

class Employee < ActiveRecord::Base
  belongs_to :business
  has_many :placements

class Placement < ActiveRecord::Base
  belongs_to :employee

class Vote < ActiveRecord::Base
  belongs_to :employee

0 个答案:

没有答案