1

I've got the following classes. They work the same way: a person can be a guardian, employee and student in many schools, and they also have one user.

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

How could I retrieve all users which belong to a specific school in just one query? To get all users which are employees, for example, I could execute (using the squeel gem):

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

I just don't know what query to write to include all three cases. Thanks for your help

sauronnikko
  • 4,665
  • 5
  • 31
  • 47

0 Answers0