Skip to content

Instantly share code, notes, and snippets.

@yonbergman
Created February 19, 2011 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yonbergman/835007 to your computer and use it in GitHub Desktop.
Save yonbergman/835007 to your computer and use it in GitHub Desktop.
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
# 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