My question is about defining cancancan ability in this following context.
I have a common model with many to many relationship between User and Company entities
class User < ApplicationRecord
has_many :company_users, dependent: :destroy
has_many :companies, through: :company_users
enum role:[:user, :admin, :superadmin]
end
class Company < ApplicationRecord
has_many :company_users, dependent: :destroy
has_many :users, through: :company_users
end
class CompanyUser < ApplicationRecord
belongs_to :company
belongs_to :user
end
I want now to define cancan ability in order to authorized current user to manage all the users belonging to the same companies as him.
I have no problem with other many to many schemas (eg : many to many between Device and Company) like this
can :manage, Device, :companies => {:users => {:id => user.id}}
is working fine !
but
can :manage, User, :companies => {:users => {:id => user.id}}
let me see, of course, only the current user because it is on the same users table.
How can I manage easily this ability between users belonging to a same company preserving the many to many relationship ?
Thanks for your help