我有以下的關聯將使用與的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