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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:01:52+00:00 2026-05-26T07:01:52+00:00

We have a UI that lets admins change permissions on our users. So we

  • 0

We have a UI that lets admins change permissions on our users. So we store these permissions in the database.

Our ability class is:

class Ability
include CanCan::Ability

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

if user.role? :super_admin
  can :manage, :all
else
  if user.role? :live_trader
    can :admin, LiveEvent
  end
  if user.role? :horse_trader
    can :horse_admin, Event
  end
  if user.role? :channel_admin
    can :manage, Channel
  end
  if user.role? :device_admin
    can :manage, Device
  end
  if user.role? :ost
    can :read, Customer.first
  end

  can do |action, subject_class, subject|
    user.roles.find_all_by_action(aliases_for_action(action)).any? do |role|
      role.authorizable_type == subject_class.to_s &&
        (subject.nil? || role.authorizable_id.nil? || role.authorizable_id == subject.id)
    end
  end

  # can always manage ourselves
  can :manage, user

end
 end
end

It works fine, except that we can’t use accessible_by. We get:

The accessible_by call cannot be used with a block 'can' definition.

I don’t see any other way of limiting result sets… Is this a bug or are we doing it wrong?

  • 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-26T07:01:53+00:00Added an answer on May 26, 2026 at 7:01 am

    The error message says it all, really.

    There’s two ways use cancan using database permissions. One uses the ‘can’ block, the other uses explicit ‘can’ permissions. They achieve the same thing.

    You can look at https://github.com/ryanb/cancan/wiki/Abilities-in-Database to see the two examples.

    I’d say that you’d want to rewrite your ‘can’ block to:

    user.roles.each do |role|
      if permission.subject_id.nil?
        can permission.action.to_sym, role.authorizable_type.constantize
      else
        can permission.action.to_sym, role.authorizable_type.constantize, :id => role.authorizable_id
      end
    end
    

    HTH.

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

Sidebar

Related Questions

I have an admin form that lets users create entities that require an image.
I have an Air application that lets users import jpg, png and swf files
I have a freelance web application that lets users register for events. In my
We have an admin debug console in our app that lets you enter a
I currently have a Rails Apps that lets users drag and drop certain elements
I have a simple page in a PHP/MySQL web application that lets admin users
I have a form that lets users create new records, In the field that
Let's say that I have a record in the database and that both admin
I have an app that lets the user search over a number of fields
Does Eclipse have a feature that lets you search a project to find all

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.