cancan icon indicating copy to clipboard operation
cancan copied to clipboard

Access different Model from a controller

Open imsazzad opened this issue 9 years ago • 3 comments

suppose I have a controller A, and an action b in there. from b i access an account by params. But I have to check this account belongs to this user or not. What I have to write in ablity class. I have tried many things. but failed. for example can :b , Account do |portfolio| portfolio && portfolio.user_id == user.id end any suggestions ?

imsazzad avatar Feb 01 '15 07:02 imsazzad

Here is the way to go:

can :b, user_id: user.id

Appropriate docs section https://github.com/ryanb/cancan/wiki/Defining-Abilities

BTW: I believe you should ask such question on StackOverflow with cancan tag. BTW2: This gem is not maintained anymore, use cancancan instead.

freemanoid avatar Feb 01 '15 10:02 freemanoid

Sorry I failed to make you understand the scenario. I have a controller PortfolioGenerator. Here is a mehtod portfolio. here get a account_id as a param. Account is a different model. So I have to access this model resources. in account table there is a user_id column also.

So what should I write?

can :portfolio, user_id: user.id () if I write this how could cancan know it is for Account model ? and :portfolio is an action of PortfolioGenerator Controller ?

imsazzad avatar Feb 02 '15 04:02 imsazzad

It would be better if you clean up your code(related models and controller) and post a gist.

freemanoid avatar Feb 02 '15 11:02 freemanoid