Last Updated: February 25, 2016
·
1.948K
· nfedyashev

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

References:
GitHub
Rubygems

1 Response
Add your response

How are the enumerations stored in the database? Should the columns be strings or integers?

over 1 year ago ·