Enumerated attributes with I18n and ActiveRecord/Mongoid support
Have you ever written a custom enumerated attributes methods or view helpers?
Tiring... boring...
There is a simpler way:
use enumerize
DSL is pretty simple:
class User
extend Enumerize
enumerize :sex, in: [:male, :female]
end
add locale token:
en:
enumerize:
user:
sex:
male: "Male"
female: "Female"
get attribute value:
@user.sex_text # or @user.sex.text
Written by Nikita Fedyashev
Related protips
1 Response
How are the enumerations stored in the database? Should the columns be strings or integers?
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#