[Rails] Enum is easier to use! Enumelize!

What is enumelize?

An enum-type attribute with support for I18n and ActiveRecord / Mongoid / MongoMapper / Sequel. It is used when you want to classify a model by status.

Setting method

  1. Add the following to the Gemfile.

gem 'enumerize'

Also execute this.


$ bundle install
  1. Add the following description to the model.

class User
  extend Enumerize

  enumerize :sex, in: [:male, :female]
end
  1. Write the following in the yaml file.

ja:
  enumerize:
    user:
      sex:
        male:Man
        female:woman

How to use

--If you want to output the value


User.sex.values  # => ["male", "female"]

User.sex.options # => [["Man", "male"], ["woman", "female"]]

user = User.new

user.sex_value # => "male"
user.sex_text # => "Man"

--If you want to use it as a select box


<%= form_for @user do |f| %>
  <%= f.select :sex, User.sex.options %>
<% end %>

reference

How to use Enumerize with Rails

Translated the README for gem enumerize

Recommended Posts

[Rails] Enum is easier to use! Enumelize!
[Rails] How to use enum
How to use rails join
[Rails] How to use authenticate_user!
[Rails] How to use Scope
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] How to use flash messages
Function is very easy to use
How to use Ruby on Rails
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
How to use Java enum type
How to use custom helpers in rails
[Ruby on Rails] How to use CarrierWave
[Rails] How to use rails console with docker
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
How to use MySQL in Rails tutorial
Compared to rails, laravel is ... (miscellaneous feeling)
[Ruby on Rails] How to use redirect_to
[Note] How to use Rails 6 Devise + cancancan
[Rails] How to use video_tag to display videos
[Rails] How to use helper method, confimartion
How to use credentials.yml.enc introduced in Rails 5.2
[Rails] How to handle data using enum
[Rails] How to use select boxes in Ransack
How to use rails g scaffold, functions, precautions
Add a tag function to Rails. Use acts-as-taggable-on
How to use enum (introduction of Japanese notation)
Super easy way to use enum with JSP
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
[Ruby on Rails] How to use session method
[Rails] How to use PostgreSQL in Vagrant environment
[Rails] Use jQuery
What to do when Address already in use is displayed after executing rails s
[Ruby on Rails] How to write enum in Japanese
Use Extend (Concerns) in Rails to standardize Controller processing.
I want to use a little icon in Rails
I used jnr-ffi (made it easier to use, etc.)
Beginners use ubuntu in windows to prepare rails environment
Use stream to verify that SimpleDateFormat is thread unsafe
How to use Map
How to write Rails
How to use rbenv
Introducing CircleCI to Rails
Introducing Bootstrap to Rails 5
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
Introducing Bootstrap to Rails !!
How to use collection_select
PATH to use docker-slim
Introduce Vue.js to Rails
Rails is Owakon www
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct