I have something that looks like this in my ability class
def initialize(staff)
staff ||= Staff.new
can :manage, Store do |store|
store.staff_privileges.select(&:owner?).map(&:staff_id).include? staff.id
end
end
I am not sure why staff.can? :manage would return true here because I thought the above block should only get executed on the instance of store and not on the class itself
staff = Staff.first
staff.can? :manage, Store #true
staff.can? :manage, Store.first #false, because there is no staff_privileges associated to this store
From https://github.com/ryanb/cancan/wiki/Defining-Abilities-with-Blocks
Why would this be? I don’t know, but I think the answer is in the “such as in the index action” bit in there? Without that behavior, the load_and_authorize_resource method cancan provides would not work for the index action.