I'm using Pundit to authorize actions in my controllers. My first try was to authorize the model in an after_action hoook:
class CompaniesController < InheritedResources::Base
after_action :authorize_company, except: :index
def authorize_company
authorize @company
end
This let me use the default controller actions which define @company
so I wouldn't hit the database twice. But, this is bad for destructive actions because it's going to not authorize the action after I've already messed up the database.
So, I've changed to using a before_action
hook:
class CompaniesController < InheritedResources::Base
before_action :authorize_company, except: :index
def authorize_company
@company = Company.find(params.require(:id))
authorize @company
end
Now, I'm not allowing unauthorized people to delete resources, etc... but I'm hitting the database twice. Is there anyway to access @company
without hitting the database twice?