Naps62
Naps62

Reputation: 970

Using scopes to limit abilities in CanCan

The CanCan documentation on this seemed a bit confusing.

It provides this example:

can :read, Article, Article.published do |article|
  article.published_at <= Time.now
end

where published seems to be a scope. But isn't the block redundant? It's querying only for published articles, and then the block is filtering for articles based on the current Time, which seems to be exactly the same, assuming there are no articles published in the future

I'm currently doing something like this:

can :validate, User.without_validation_finished
can :invalidate, User.with_validation_requested

And it seems to be working as expected, i.e., for users outside of those scopes, i don't get access to those actions. Should i use a scope like i'm currently doing, or change this to a block? I imagine this might also have some performance differences

Upvotes: 0

Views: 1024

Answers (1)

Logan Serman
Logan Serman

Reputation: 29870

The scope is used for the index action, because there is no singular article (@article) present. The block is used for the show action, when @article is present.

Upvotes: 1

Related Questions