最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

associations - Rolify and Resourcify for User STI in Rails - Stack Overflow

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论