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 6359551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:31:59+00:00 2026-05-24T23:31:59+00:00

I am using cancan with one role per user. The roles are integers instead

  • 0

I am using cancan with one role per user. The roles are integers instead of strings. My ability.rb is organized like this:

def initialize(user, session)
    if user.role.to_i == 9
        can :create, [Resource1, Resource2, Resource3]
        can :update, [Resource1, Resource2, Resource3]
    elsif user.role.to_i == 8
        can :create, [Resource1, Resource2]
        can :update, [Resource1, Resource2]
    else
        can :create, Resource1
        can :update, Resource1
    end
end

In reality there are 7 roles instead of 3 and the file is much more complex. Could the file be rewritten like this instead so a role can be defined cumulatively through the conditional statements?

def initialize(user, session)
    if user.role.to_i >= 9
        can :create, Resource3
        can :update, Resource3
    end
    if user.role.to_i >= 8
        can :create, Resource2
        can :update, Resource2
    end
    can :create, Resource1
    can :update, Resource1
end

I’d like to know if this EXACT structure with the conditional statements would work before i rewrite the entire file. Thanks.

  • 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-24T23:32:00+00:00Added an answer on May 24, 2026 at 11:32 pm

    Yes (except for the comparison). Look here, it adds the rule.

    def can(action = nil, subject = nil, conditions = nil, &block)
      rules << Rule.new(true, action, subject, conditions, block)
    end
    

    Edit: In case it may be useful.

    def initialize(user, session)
      resources = [Resource1]
      resources << Resource2 if user.role.to_i >= 8
      resources << Resource2 if user.role.to_i >= 9
      can :create, resources
      can :update, resources
    end
    

    Edit 2:

    What does the first part mean?

    In first place, sorry for pasting the wrong chunk of code. Then, you can see you are adding a new rule whose subject is the one you passed to can. In Rule initializer, you can see the line:

    @subjects = [subject].flatten
    

    that means that if you had an array you still have that array (that’s what flatten is for), but if you had only one resource you get a one-item array. You can dig more in the code and you will notice than a rule with one resource behaves as a rule with only one.

    Then this rule is added to rules without replacing any other.

    Hope is clear now.

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

Sidebar

Related Questions

I have two entities. User and Role. I am using Devise and CanCan. They
I'm quite new to this and I'm using cancan + devise for my user
I'm using cancan as my authorization engine. I already have the roles in user:
Using RSpec and Cancan, I have this test which fails with: Failure/Error: User.should_receive(:new).and_return(@user) expected:
This is the relevant part of my ability.rb class: def initialize(user) can :manage, User
I'm using CanCan in a project to manage different role level on each entity
I'm using Cancan, Devise, Rails 3 for my ordering application. Each user has many
I'm using Cancan to control User abilities, and have recently run into an odd
I am currently using cancan with rspec. Please take a look at my ability.rb
Using C#, I need a class called User that has a username, password, active

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.