Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7408645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:57:06+00:00 2026-05-29T05:57:06+00:00

This is the relevant part of my ability.rb class: def initialize(user) can :manage, User

  • 0

This is the relevant part of my ability.rb class:

def initialize(user)
  can :manage, User do |u|·
    user == u·
  end
end

According to cancan documentation:

The block is only evaluated when an actual instance object is present.
It is not evaluated when checking permissions on the class (such as in
the index action).

However, for the UsersController index action, even though the block isn’t run, it still grants :manage permissions, listing the users.

I have confirmed this by commenting out the above code, and then the user is not authorized.

As far as I understood cancan, the block is supposed to to run (which indeed it doesn’t), and if it doesn’t run, then the block is considered to have returned false, thus not granting the permission.

Am I getting this wrong, or is this a bug?

I’m using cancan version 1.67.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:57:07+00:00Added an answer on May 29, 2026 at 5:57 am

    I think you are misreading the documentation here:

    If no instance is evaluated but rather a Class cancan will NOT execute the block as there is no instance check to be performed.
    This saves you the hassle of making the block nil resilient.
    Since the block is never executed there is no assumption made what it returns and cancan will only look at it’s Class rules.

    If it would call the block you’d have to account for this (in a slightly more complex model):

    can :manage, Post do |p|
      p.category.owner == user   #this will blow up because of nil
    end 
    

    I guess the default is buried here, but this seems like a sensible default.
    If you are going to allow a user to manage his own posts anyway you should prevent him from seeing other’s posts in the index anyway. So the only threat to you is him accessing a resource he explicitly has no access to.
    (Cancan will never hook up on normal ActiveRecord queries and filter them down by abilities. So User.all will always return all users – regardless of ability)

    However: If you are trying to implement something like a profile, I’d suggest you just change around your UsersController logic a bit and simply make the profile a singular resource as described in the Rails Guides

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be a simple question, but I just can't recall the relevant API.
I have class where the relevant part looks like class C { void Method<T>(SomeClass<T>
This is the relevant part of my code (I am trying to add a
How Do You Turn On Parenthesis Matching in Visual Studio 2005? This is relevant
I tried to find this in the relevant RFC, IETF RFC 3986 , but
I don't know if it is relevant that this is happening in an MVC
I'm specifically referring to JavaScript anonymous function but this could be relevant to other
I have two tables (well, two relevant for this question) : Bets (holds the
Though it's on the edge of programming questions, I think this is still relevant
This should be a common scenario, but could not find any relevant post yet..

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.