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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:37:02+00:00 2026-05-26T14:37:02+00:00

I was wondering how I can define an ability class and serve that ability

  • 0

I was wondering how I can define an ability class and serve that ability class depending on the user that has logged in.

I am using Active Admin, Can Can and Devise and I have successfully created a User and an AdminUser models.

I have this in my ability.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new

    if (user)
      can :manage, Item
    end
  end
end

Now I have used this wiki entry to determine that we can indeed define a custom ability file and use that instead of the ability.rb:

https://github.com/ryanb/cancan/wiki/changing-defaults

But what I wanted to do is, be able to use ability.rb if a “non-admin user” is signed in and a custom abilty if a user admin is signed in.

Side Question: Could it be done such that I don’t need a custom one and I could set permissions in one ability.rb file?

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

    I’ve never really used ActiveAdmin, so I’m not entirely sure if I’m missing something, but it doesn’t seem like that framework relies on CanCan. That’s why I’m assuming you’re defining a current_ability method like explained in the wiki and it’s instantiated with Ability.new(current_user).

    If that’s the case, and your current_user can be either a User or an AdminUser, then there’s no problem in checking for that in the Ability class:

    class Ability
      include CanCan::Ability
    
      def initialize(user)
        user ||= User.new
    
        if user.kind_of? AdminUser
          can :manage, Item
        elsif user.kind_of? User
          can :read, Item
        end
      end
    end
    

    You can simply take a look at the user’s type and change the rules accordingly. You can also use is_a? instead of kind_of? for stricter checking, but it’s probably not required and might cause issues if you decide to do inheritance later on.

    Another way you could check is by defining an admin? method in both models. This might be a better way to do it, since explicit type checking is not very popular in ruby — it often limits your choices. It might look like this:

    class User < ActiveRecord::Base
      def admin?
        false
      end
    end
    
    class AdminUser < ActiveRecord::Base
      def admin?
        true
      end
    end
    
    class Ability
      include CanCan::Ability
    
      def initialize(user)
        user ||= User.new
    
        if user.admin?
          can :manage, Item
        else
          can :read, Item
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if someone can explain this macro to me. #define Q_DECLARE_PRIVATE(Class) \
I am just wondering why can't I define a static class as protected, private
I am wondering how I can define an object in C whose reference will
I'm wondering how can I submit a form via Ajax (using prototype framework) and
I was wondering how can I list the user tables name with its primary
I was wondering how can I validate a single checkbox using PHP and MySQL.
I've got a class that has a read-only property defined that is actually a
I was wondering if you can define your own attributes in HTML. For example
When using C preprocessor one can stringify macro argument like this: #define TO_STRING(x) a
I'm wondering if I can define some functions in a header file and then

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.