Check user has order create permission for multiple companies - ruby

54 views Asked by At

The association is as follows

Company has_many :orders

The permissions are set as follows

 if @resource.has_cached_role?(:client_admin)
   can %i[read create confirm], Order, company_id: resource_company_ids
 end



  def resource_company_ids
    @resource_company_ids ||= Company.where(id: @resource.company_id)
      .or(Company.where(parent_id: @resource.company_id))
      .pluck(:id)
  end

#1) The client admin logging in to a company can create orders for that company and also for its child companies.

#2) Also, there are company which does not have child companies.

When a user is logged in, i need to check if the user has order create permissions for multiple companies(scenario #1)

How to achieve this using cancancan?

Any help would be appreciated.

1

There are 1 answers

0
Lam Phan On

i think a block conditions maybe help

can %i[read create confirm], Order |order|
  resource_company_ids.include?(order.company_id)
end