Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Access different Model from a controller #1026

Open
imsazzad opened this issue Feb 1, 2015 · 3 comments
Open

Access different Model from a controller #1026

imsazzad opened this issue Feb 1, 2015 · 3 comments

Comments

@imsazzad
Copy link

imsazzad commented Feb 1, 2015

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 ?

@freemanoid
Copy link

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.

@imsazzad
Copy link
Author

imsazzad commented Feb 2, 2015

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 ?

@freemanoid
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants