can :manage, Company, :company_id => user.company.idcan :manage, Company, :company => { :company_id => user.company.id }can :manage, Company, :company => { :company => { :company_id => user.company.id } }
class Company < ActiveRecord::Base
# ..
def subsidary_of
# A list of companies under this company... recursively in Rails,
# raw SQL (recursive self referential lookup), or something like
# the ancestry gem
end
end
can :manage, Company do |company|
company == user.company || company.subsidary_of.include?(user.company)
end
def subsidiary_of supercompanies = [] unless company.nil? supercompanies << company supercompanies + self.company.subsidiary_of end return supercompanies end
The accessible_by call cannot be used with a block 'can' definition. The SQL cannot be determined for :index Company(id: integer, name: string, description: text, company_id: integer, created_at: datetime, updated_at: datetime)
Hi Christian!
Look up more about Cancancan and block syntax, to be able to load and authorize collections (:index type of routes), you need to also specify a class level scope, since we can’t run each instance through the block to see which ones are valid.
Breaks down like this:
can [:ability], Model, Model.scope_to_select_on_index_action do |model_instance|
model_instance.condition_to_evaluate_for_new_create_edit_update_destroy
end
can :manage, Company, Company.has_parent do |company| company == user.company || company.subsidiary_of.include?(user.company) end
scope :has_parent, -> { where("company_id is not null") }
def self.has_parent(company)
where(...)
end
can :manage, Company, Company.subcompanies_of(user.company) do |company| company == user.company end
def self.subcompanies_of(the_company) subcompanies = [the_company] unless the_company.companies.nil? the_company.companies.each do |subcompany| subcompanies = subcompanies + Company.subcompanies_of(subcompany) end end return subcompanies end
undefined method `include?' for #<Company:0x007fe4648d3768>