Great! In the users table, we now have a column named role that we can use to assign different roles to users, such as “editor” or “admin”.



Let’s add a method to the User model that will help us use the role column in our application. Within the class User, beneath the has_secure_password method, type

def editor? self.role == 'editor' end

We can use this method to determine if a user has the role of editor.


Open db/seeds.rb. We’ve added a user named Mateo with the role ‘editor’. Seed the database with this data

$ bundle exec rake db:seed

We should now be able to use the editor? method to check whether a user has an editor role. First, start the Rails console by running

$ rails console

Then check whether Mateo is an editor

> mateo = User.find_by(email: '[email protected]') > mateo.editor?

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?