1

私は次のクラスを持っています。彼らは同じように機能します。多くの学校では、保護者、従業員、学生のいずれかになり、ユーザーも 1 人です。

class User < ActiveRecord::Base
  belongs_to :person
end

class Person < ActiveRecord::Base
  has_many :guardians
  has_many :employee
  has_many :students
end

class Guardian < ActiveRecord::Base
  belongs_to :person
  belongs_to :school
end

class Student < ActiveRecord::Base
  belongs_to :person
  belongs_to :school
end

class Employee < ActiveRecord::Base
  belongs_to :person
  belongs_to :school
end

特定の学校に属するすべてのユーザーを 1 回のクエリで取得するにはどうすればよいですか? たとえば、従業員であるすべてのユーザーを取得するには、( squeelgem を使用して) 実行できます。

User.joins { person.employees }.where { person.employees.school_id == school_id }

3 つのケースすべてを含めるには、どのクエリを作成すればよいかわかりません。ご協力いただきありがとうございます

4

0 に答える 0