I have a bunch of controllers that extend the ApplicationController
and each one loads a different cancan resource, e.g. UsersController
-> @user
, PostsController
-> @post
. I was wondering, is it possible to reference the resource from the ApplicationController without knowing the instance variable name? Something like resource_instance
.
Ok, I got my final answer and it's No. I explored cancancan, cancan's well maintained spiritual child and the code is there, but is not meant to be used by the end user.
You could get what you want, it isn't pretty:
self.class.cancan_resource_class.new(self).send(:resource_instance)
That said, please don't. cancan_resource_class
isn't documented to to be used by end users and :resource_instance
is protected, hence the send
. The developers could choose to change this and break your application.
If all have a company
, they belong too, I recommend you nest the routes, so all routes have a :company_id
you could use to get the @company
object you want: companies/:company_id/users
, etc.