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

  • Home
  • SEARCH
  • 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 7706303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:05:30+00:00 2026-06-01T00:05:30+00:00

Firstly here is my ruby and rails version with a couple of the important

  • 0

Firstly here is my ruby and rails version with a couple of the important gem versions.

  • Ruby 1.9.3-p125
  • Rails 3.2.2
  • Devise 2.0.4
  • CanCan 1.6.7

I used the template from Daniel Kehoe to get the project up and running quickly.

$ rails new myapp -m https://raw.github.com/RailsApps/rails3-application-templates/master/rails3-devise-rspec-cucumber-template.rb -T

My problem:

I have an administration section for creating new users and two roles with this ability. I have a requirement, admin users should not be able to create superadmin users.

When an admin logs in and clicks on the new user button the roles list is filtered to remove the superadmin option, if I logout of the admin role and login as a superadmin then create a user I have the filtered list from the admin. If I refresh 5-6 times I get the correctly filtered list.

User model

# /app/models/user.rb
class User < ActiveRecord::Base
  ROLES = { 
    superadmin: 'superadmin', 
    admin:      'admin'
  }

  def role?(role)
    self.role == role.to_s
  end
end

User controller

# /app/controllers/admin/users_controller.rb
module Admin
  class UsersController < BaseController
    load_and_authorize_resource

    def new
      @user = User.new
      @roles = User::ROLES

      if current_user.role? :admin
        @roles.delete :superadmin
      end
    end
  end
end

View

# /app/views/admin/users/_form.html.haml
= form_for [:admin, @user], html: { class: 'form-horizontal' } do |f|
  ...
  .control-group
    = f.label :role, class: 'control-label'
    .controls
      = f.select :role, @roles, selected: :admin

If anyone can point me in the right direction as to why the @roles is retaining its value between login/logouts for users with different roles but then being cleared that would be really helpful thanks, I’ve been stumped on this for over an hour as has a few others.

  • 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-06-01T00:05:32+00:00Added an answer on June 1, 2026 at 12:05 am

    the line @roles = User::ROLES is getting a reference to User::ROLES instead of a copy. So, when you call @roles.delete :superadmin, you’re modifying User::ROLES, too.

    I’m guessing after 5-6 requests, your request is served by a different process whose global copy has not (yet) been modified.

    To solve the provlem, change @roles = User::ROLES to @roles = User::ROLES.dup

    Or, change @roles.delete :superadmin to @roles = @roles.except(:superadmin)

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

Sidebar

Related Questions

I'm getting some rather unexpected behavior from hashes in ruby here's a simplified demonstration
I have a button on my Ruby on Rails view file. Firstly when the
So firstly here's my query: ( NOTE:I know SELECT * is bad practice I
Firstly, here's my script: #!/usr/bin/python import sys, os sys.path.append('/home/username/python') sys.path.append(/home/username/python/flup) sys.path.append(/home/username/python/django) # more path
Firstly, let me start by saying that I haven't ever directly used SQL views.
Firstly, here's my Makefile, it's really, really cool. It generates an executable for every
Here is a scenario: Desktop application Installed from the web Needs to call a
This Question arises from a Q&A here I have some doubts that i think
Firstly here is the problem: A positive integer is called a palindrome if its
So firstly here is the relevant table structure: TUBE_VIDEOS ------ id video_name TAGS ----

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.