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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:56:46+00:00 2026-05-21T14:56:46+00:00

In my app I have the following models: Users Groups Permissions (user_id, group_id, role_id)

  • 0

In my app I have the following models:

Users
Groups
Permissions (user_id, group_id, role_id)

Where role_id 1: admin, 2: member

I want to make sure I’m understanding CanCan correctly. In the ability.rb file, I only want group admins (permission.role_id == 1) to be able to update/destroy/create new group permissions.

permission.role_id == 2, members, should just be able to read the group and the group’s permissions. Except for having the ability to destroy their group permission.

Here is my CanCan ability.rb file:

class Ability
    include CanCan::Ability

    def initialize(current_user, groupid_viewing)

        current_user ||= User.new #Guest user (not signed in)

        if groupid_viewing && current_user.try(:role, groupid_viewing) == 'Admin'
            can :manage, Group

            can [:create, :update], Permission do |permission|
        current_user.try(:role, groupid_viewing) == 'Admin'
            end





class GroupsController < ApplicationController
....
    def current_ability
          @current_ability ||= Ability.new(current_user, params[:group_id] && params[:group_id].to_i)
    end


class ApplicationController < ActionController::Base
    def current_ability
        @current_ability ||= Ability.new(current_user, nil) #(user, group)
    end
  • 1 1 Answer
  • 3 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-21T14:56:47+00:00Added an answer on May 21, 2026 at 2:56 pm

    You also need to specify the abilities for the role_id:2.

    if groupid_viewing && current_user.try(:role, groupid_viewing) == 'Member'
       can :read, Group
       can :destroy, Permission do |permission|
          current_user.try(:role, groupid_viewing) == 'Member'
       end
    

    Also, there is no need of creating current_ability the way you are doing.
    It should be an after_create callback that should assign abilities when the Member or Admin is created.

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

Sidebar

Related Questions

I have the following model in my Django app: class Group(models.model): name=models.CharField(max_length=30) users=Models.ManyToManyField(User) In
I have the following models in my Django app. How can I from the
I have the following Node.js directory structure. |--app/ |--app.js |--routers/ |--index.js/ |--models/ |--schemas.js |--post.js
I want to create some tests for my app and I have the following
I have the following code: # app/models/part.rb class Part < ActiveRecord::Base has_many :lots #
I have a model in my dJango app called event . I want users
I use mongoose in my node.js app, and basically have the following models: //
I have the following mailing code in app/models. class Mailman < ActionMailer::Base default :from
I have a rails app with two models, visits and users. A visit has
I have an app with the following models: User , Task , and Assignment

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.