I am using devise as a user model with rolify for roles
A user has one role called 'client'
A user has one role called 'agent'
I want to make a association like this
class User < ApplicationRecord
rolify
has_many :agents # if user role is client will get all agents
has_many :clients # if user role is agent will get all clients
end
How to make above associations. Please help me. Thanks
I solved the above problem with following associations. Let me know if I am wrong
class User < ApplicationRecord
rolify
has_many :user_agents
has_many :agents, through: :user_agents # if user role is client will get all agents
has_many :user_clients, class_name: 'UserAgent', foreign_key: :agent_id
has_many :clients, source: :user, through: :user_clients # if user role is agent will get all clients
end
# field: user_id and agent_id
class UserAgent < ApplicationRecord
belongs_to :user
belongs_to :agent, class_name: 'User'
end