我有以下的关联将使用与的has_many组:无法通过导轨5
class Student < ApplicationRecord
has_many :people_schools
has_many :schools, through: :people_schools
end
class PeopleSchool < ApplicationRecord
belongs_to :student
belongs_to :school
end
class School < ApplicationRecord
has_many :people_schools
has_many :students, through: :people_schools
end
我试图让他们通过学校组织的学生名单。我曾尝试以下:
Student.joins(:schools).all.group('schools.name')
,但我得到了以下错误:
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR: column "students.id" must appear in the GROUP BY clause or be used in an aggregate function
我该如何解决这个问题?
学生可以成为许多学校的学生('has_many:schools'),还是他总是一个学生('has_one:school')?甚至没有连接表的“belongs_to:school”? – ulferts