I’m using Rails 3.0.9 with cancan 1.6.7 and device 1.4.8
I’ve a doubt that, Does the can / can? / cannot / cannot? method invoke the current_ability method because
I just wrote the current_ability method in application controller which overwrites the cancan’s current_ability method
In apllication_controller.rb
def current_ability
if current_user.kind_of(User)
@current_ability ||= UserAbility.new(current_user)
else
@current_ability ||= AdminAbility.new(current_admin)
end
end
Found that, this method is invoked 44 times per page request
the index page loaded after login contains 35 menu items (loaded using can? method) and content with 3 actions (loaded using can? method)
why current_ability method is invoked 44 times ?
Yes. Every
can?cannot?andauthorize!calls thecurrent_abilitymethod (See: CanCan’s controller_additions.rb)Also, I wouldn’t worry about the 44 calls to
current_ability. You’re only creating one Ability object per request because you are using an instance variable at the controller, with the||=operator (i.e. memoization). It’s not a significant performance hit.For example, let’s do a quick benchmark to fetch the same cached object 100,000 times…