Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Make the 1st user admin

How can I make the 1st user admin in Rails using Devise? I am new to Rails so please forgive me if this is a simple fix.

Thank you!

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

1) Do you have a field in your User model that would allow people to be admins? For example an "admin" column which is of type Boolean? 2) If so, you can do this manually in the console.

Let me know which of the above I should explain first.

Hi Maciej,

I have a migration file for adding Admins however, I have nothing in my model/user. See below.

db/migrate

class AddAdminToUsers < ActiveRecord::Migration

def self.up add_column :users, :admin, :boolean, :default => false end

def self.down

remove_column :users, :admin end end

I'm not sure if this is correct.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, this seems fine. Make sure you run rake db:migrate and have a user created. Now in your terminal/console run 'rails c' and type 'User.all'. This should show you all the users that you have registered. They should all have the field 'admin' set to false. You can grab the first user by typing 'user1 = User.first'. Now when you type 'user1' and hit enter, it should only show you this one user, with admin field still saying false. Now type 'user1.admin = true', hit enter and then 'user1.save'. This should give this user the admin role. Check this again by running 'User.all' command. If everything is fine, type 'exit', hit enter and you're done.

This worked perfectly! Thank you so very much!