Skip to content

Instantly share code, notes, and snippets.

@Stenerson
Created December 15, 2013 06:19
Show Gist options
  • Save Stenerson/7969619 to your computer and use it in GitHub Desktop.
Save Stenerson/7969619 to your computer and use it in GitHub Desktop.
I'm storing user roles as integers but I don't have a table yet stating what a role means. For now it's just being kept in the user model. I needed a (non localized) string saying what the user's role was. I thought the case method was too ugly so I went this direction instead.
# u = User.first
# u.role_id # => 1
# Desired output
# u.role # => "Administrator"
# Obvious implementation
def role
case self.role_id
when 1
"Administrator"
when 2
"Registered User"
when 3
"Trial"
when 4
"Interviewer"
else
"Not Valid"
end
end
# Cleaner implementation
def User.roles
["Administrator", "Registered User", "Trial User", "Interviewer"]
end
def role
User.roles[self.role_id-1]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment