I have the following model
class Event < ActiveRecord::Base
has_many :attendances
class Attendance < ActiveRecord::Base
belongs_to :user
class Student < User
has_one :student_detail
class StudentDetail < ActiveRecord::Base
belongs_to :school
class Staff < User
has_one :staff_detail
class StaffDetail < ActiveRecord::Base
The StudentDetail and StaffDetails have additional information, I am trying to avoid having it all in one STI user table due to having to work with something similar to concrete class per table pattern
I can do this easily enough
Event.includes(:attendances => :user).where(...)
but I want to be able to includes depending on user type e.g.
Event.includes(attendances: {:user => :student_details })
This will fail as some of the users are Staff objects.
I realise rails won't support this out of the box, but anyone have any tricks to get this to work
best solution right now would be split user on attendance to student and staff i.e.
class Attendance < ActiveRecord::Base
belongs_to :student, -> {includes(:staff_detail) }
belongs_to :staff, -> {includes(:student_detail) }
#belong_to :user
which isn't ideal. Anyone have any tips? way to solve this.
ActiveRecord::Associations::Preloader.new.preload(records, associations)
. See commit for details. – Slesvig