Created
February 19, 2011 11:21
-
-
Save yonbergman/835007 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
enum :role, [:admin,:moderator,:user] | |
# You can optionally add this function to your model | |
# and it will be called after each change of the role for callbacks you might need | |
def role_changed(from,to) | |
puts "The role changed from #{from} to #{to}" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Access through enum name | |
user.role #returns the user's current role | |
user.role = :admin # set the user's role to admin | |
# Access through enum values | |
user.admin? # returns true if user is currently an admin | |
user.moderator! # set the user's role to moderator and saves the user | |
# General enum functions | |
User::ROLES # returns all available roles of the user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment