Mark Robinson
Mark Robinson

Reputation: 1629

Rails CanCan hash of conditions access denied

I'm having an issue setting up an ability that looks like this:

can :read, Project, :allocate_to => user.id

The problem is

ability.can?(:read, my_project)

returns false even though

Project.accessible_by(ability)

returns just the projects for that user as expected.

I've tried cancan 1.6.10 and cancancan 1.9.2 on rails 3.2.19 with ruby 1.9.3-p547

Is this a bug with cancancan or my code? How do I find out why ability.can?(:read, my_project) is failing?

Also posted as an issue

Upvotes: 0

Views: 242

Answers (1)

CDub
CDub

Reputation: 13354

I've run into issues using the hash notation in cancan with abilities like this one. Have you tried:

can :read, Project do |project|
  project.allocate_to == user.id
end

I'm not sure why this is the case, but this has worked for me from time to time.

Upvotes: 0

Related Questions