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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:01:52+00:00 2026-06-01T13:01:52+00:00

I have a sample Rails 3.1.1 application that I have set devise up to

  • 0

I have a sample Rails 3.1.1 application that I have set devise up to manage the user accounts etc.

I ran the following steps to add an admin attribute to the user table:

$ rails generate migration add_admin_to_user admin:boolean

Added the following to my migration:

class AddAdminToUser < ActiveRecord::Migration
  def self.up
    add_column :users, :admin, :boolean, :default => false
  end

  def self.down
    remove_column :users, :admin
  end
end

I then ran the db:migrate and added the following to my layout file:

<% if current_user.admin? %>
  You are ADMIN.
<%end %>

Then, to add the first admin user I used the following seed file:

puts 'SETTING UP DEFAULT USER LOGIN'
user = User.create! :name => 'Test User', :email => 'test@test.com', :password => 'password', :password_confirmation => 'password'
puts 'New user created: ' << user.name

That worked, so I then adapted it with the admin field:

puts 'SETTING UP DEFAULT USER LOGIN'
user = User.create! :name => 'Test User', :email => 'test@test.com', :password => 'password', :password_confirmation => 'password', :admin => 'true'
puts 'New user created: ' << user.name

The above seed file worked, but the admin flag isn’t being shown.

Have I missed something?

Update: model/user/rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me
end
  • 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-01T13:01:53+00:00Added an answer on June 1, 2026 at 1:01 pm

    Don’t add admin to attr_accessible. This could lead to a major security breach. A malicious user could send a PUT request as follows:

    put /users/17?admin=1
    

    which would grant user with id 17 admin privileges. The whole point of attr_accessible is to define which attributes are accessible to your model. This one in particular is probably not one you want available.

    Instead, I suggest that you create your sample user with a rake file. Place a file (user_data.rake) in lib/tasks with the following and it should do the trick.

    namespace :db do
      desc "Fill database with sample data"
      task populate: :environment do
        admin = User.create!(name: "Test User",
                     email: "test@test.com",
                     password: "password",
                     password_confirmation: "password")
        admin.toggle!(:admin)
      end
    end
    

    This defines a task db:populate and should be all you need for creating your sample user.

    Having already done a db:migrate, just run db:reset, db:populate.

    Note the admin.toggle!. That is where the magic happens.

    The toggle method should be used with caution since it bypasses callbacks and validations you have defined for your model. In the case I’ve mentioned, since you are using it from a rake task manually there is no risk that it could be used maliciously in mass-assignment. You can find more information on the toggle method here: http://apidock.com/rails/ActiveRecord/Base/toggle

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

Sidebar

Related Questions

I have been developing a sample application after reading Rails 3 Tutorial book. In
I have a really simple Rails application that allows users to register their attendance
I have a Ruby on Rails application that has two active environments, Stage and
I have built a custom administration panel into a Rails application that allows content
I have a rails application in which a user creates a record called a
I have created a rails application that has a simple RESTful json API. This
I have a simple rails application in which I'm trying to add a very
I have a Rails application that I want to be able to jump to
I have a Rails simple application that has two main models. A person model
I have a Rails application that allows users to import information from various sources

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.