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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:06:28+00:00 2026-05-28T14:06:28+00:00

I have looked at declarative_authorization, CanCan, and CanTango. They all are good in adding

  • 0

I have looked at declarative_authorization, CanCan, and CanTango. They all are good in adding authorization to the application but I was wondering how does one add authorization to specific instance of a model i.e. a person can have a manage access in one project and only limited (read less than manage: limited update, etc) in another.

Could you please a better way? Apologies if my question sounds too trivial. It could be because I am new to RoR.

thanks,
John

  • 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-28T14:06:28+00:00Added an answer on May 28, 2026 at 2:06 pm

    As I know CanCan and declarative_authorization, and I implemented role-based authorizations with both, I recommend CanCan. Just my two cents.

    Example (untested, unfortunately I cannot test here and I have no access to my code)

    So let’s say we have a structure like this:

    class User < ActiveRecord::Base
      belongs_to :role
    end
    
    class Role < ActiveRecord::Base
      has_many :users
    
      # attributes: project_read, project_create, project_update
    end
    

    Then, CanCan could look like this:

    class Ability
      include CanCan::Ability
    
      def initialize(user)
        @user = user
        @role = user.role
    
        # user can see a project if he has project_read => true in his role
        can :read, Project if role.project_read? 
    
        # same, but with create
        can :create, Project if role.project_create?
    
        # can do everything with projects if he is an admin
        can :manage, Project if user.admin?
      end
    
    end
    

    You can find all information you need in the CanCan wiki on github. Personal recommendation to read:

    • https://github.com/ryanb/cancan/wiki/Defining-Abilities
    • https://github.com/ryanb/cancan/wiki/Defining-Abilities-with-Blocks
    • https://github.com/ryanb/cancan/wiki/Authorizing-Controller-Actions

    Basically you just need to extend the example above to include your roles through your relations. To keep it simple, you can also create additional helper methods in ability.rb.

    The main mean caveat you may fall for (at least I do): Make sure your user can do something with a model before you define what the user can’t. Otherwise you’ll sit there frustrated and think “but why? I never wrote the user can’t.”. Yeah. But you also never explicitly wrote that he can…

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

Sidebar

Related Questions

I have looked at the usual suspects...Spark, NHaml, etc. They all seem to be
I have looked into all the suggested articles and they seem to address the
I have looked all over the internet and haven't found my answer. Whenever one
I have looked for answer on other questions but I can't find one. My
I have looked at NHibernate and EntitySpaces and they both seem to work differently.
I have looked all of the place for this and I can't seem to
I have looked at the Suggested related questions but none of them are what
I have looked and tried but don't see where I can stop some being
Have looked so long for a library specialized in dealing with iPhone Accelerometer but
I have looked at this and this which both describe similar problems but don't

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.