Ruby on rails 是否通过从组_中的每个组检索单个记录?
我正在努力让每个月在就业方面得分最高的企业的员工 架构如下所示:Ruby on rails 是否通过从组_中的每个组检索单个记录?,ruby-on-rails,ruby,postgresql,Ruby On Rails,Ruby,Postgresql,我正在努力让每个月在就业方面得分最高的企业的员工 架构如下所示: class Business < ActiveRecord::Base has_many :employees has_many :placements, through: :employees class Employee < ActiveRecord::Base attr_accessible :name belongs_to :business has_many :placements class
class Business < ActiveRecord::Base
has_many :employees
has_many :placements, through: :employees
class Employee < ActiveRecord::Base
attr_accessible :name
belongs_to :business
has_many :placements
class Placement < ActiveRecord::Base
attr_accessible :month, :employee_id, :points
belongs_to :employee
但是我得到了一个PostgreSQLgroupby
错误:
: 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
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...
任何帮助都将不胜感激。当您对所选的任何列进行分组时,您需要将其包含在“分组人”中,或者在这些列上具有某种聚合功能。默认情况下,AR会缩回所有列,因此您可能希望使用
限制这些列。选择当您收到错误时,始终显示错误的确切文本。在这种情况下,我认为您需要的可能是HAVING
子句。谢谢Craig。我添加了错误。如果您没有在语句中添加“includes”,错误是否仍然存在?是,如果我删除。includes(: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
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...