New Discussion

Notifications

You’re not receiving notifications from this thread.

CanCanCan: Defining Abilities

1
General

I'm using the CanCanCan gem. According to the documentation you can define abilities. That is working for me. What I want to do is to limit the access to records, that contain a value. Something like:

"can :crud, Order, :brand == nil"

is not working

A User should only be allowed to see the record, if the brand-column of the Order table is empty. As my example doesn't work apparently.. How would you do it?

Hello,
To limit access to records in CanCanCan based on a condition like the brand column being nil, you can use a block to define the conditions for the ability. Here's how you can set it up:

ruby
class Ability
include CanCan::Ability

def initialize(user)
can :crud, Order, brand: nil
end
end

In this example, the can method is using a hash to specify that the brand column must be nil for users to have :crud access to Order records.

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 90,592+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.