I have User STI for Administrator & Employee model and uses rolify and resoucify with the below logic
consider that I have installed Rolify correctly
class User < ApplicationRecord
end
class Administrator < User
rolify
has_many :employees, -> { where(roles: { name: :admin }) }, through: :roles, source: :resource, source_type: :Employee
end
class Employee < User
resourcify
has_many :administrators, -> { distinct }, through: :roles, class_name: 'User', source: :users
has_many :admins, -> { where(roles: { name: :admin }) }, through: :roles, source: :administrators
end
If I try in console employees works fine for me
Administrator.last.employees
[#<Employee id: 789, email: "[email protected]", created_at: "2024-11-19 11:55:43.251636000 +0530", updated_at: "2024-11-19 11:55:43.251636000 +0530", is_active: true,...
> Employee.find(789).administrators
[]
above Employee.find(789).administrators is always empty
Why the above design?
Answer: Employee record is resource of administrators, administrators can be with any role like team_leader, accountant, HR or any other