[Rails] How to handle data using enum

This time, I had the opportunity to update the status using enum, and I implemented it while struggling with various investigations. I will write about enums.

environment##

Ruby : 2.6.3 Ruby on Rails : 5.2.4

flow##

  1. What is an enum?
  2. How to write an enum
  3. Problems in implementation

1. What is an enum?

Enum can form an array, store data in the column in advance, and manage acquisition and update. It is used when the number of data and options are decided like this time.

When I implemented it, I referred to this article. -[Rails] What is an enum? I made a select box using an enum. https://qiita.com/clbcl226/items/3e832603060282ddb4fd

2. How to write an enum

This time, the order model (order) and the related order product model (order_product) have the following status. We will implement them so that they work together, but this time we will omit them. The writing method is simple and the key has a value.

order.rb


  belongs_to :customer
  has_many :order_products, dependent: :destroy

  enum status: {
Waiting for payment: 0,
Payment confirmation: 1,
In production: 2,
Preparing to ship: 3,
sent: 4
  }

order_product.rb



  belongs_to :order
  belongs_to :product

  enum making_status: {
Cannot be manufactured: 0,
Waiting for production: 1,
In production: 2,
Production completed: 3
  }

3. Update with a numerical value

When updating the status, I used form_with to write as follows.

orders/show.html.erb



 <%= form_with model: order, url: admin_order_path(order), class:"update_status" do |f| %>
    <%= f.select :status, Order.statuses.keys %>
    <%= f.submit "update", class:'btn btn-info' %>
  <% end %>

orders_controller.rb


class Admin::OrdersController < ApplicationController

  def update
    order = Order.find(params[:id])
    order.update(status: order.status)
    redirect_to admin_order_path(order)
  end

end

In this case, for example, if you select "Payment Confirmation" in the view and update it, the character string "Payment Confirmation" will be passed as it is to the parameter. It works fine, but I was wondering why it is not possible to transfer data numerically even though it is managed by enum.

orders/show.html.erb


  <%= form_with model: order, url: admin_order_path(order), class:"update_status" do |f| %>
    <%= f.select :status, options_for_select(Order.statuses.to_a, selected: order.status_before_type_cast) %>
    <%= f.submit "update", class:'btn btn-info' %>
  <% end %>

So, I tried to write the value to be passed as a numerical value like this, In this case, it was passed as a String type and the data could not be updated.

As a result of various investigations, it seems that it is necessary to convert what was passed in String type to Integer type in the controller.

orders_controller.rb


    order = Order.find(params[:id])
    status = params[:order][:status].to_i
    order.update(status: status)
    redirect_to admin_order_path(order)

You have successfully updated the data with numerical values! I wish I could make it a little more concise (maybe)

There may be various opportunities to use it in the future, so I want to acquire it firmly. We would appreciate it if you could comment if there are any deficiencies or mistakes in the content.

Recommended Posts

[Rails] How to handle data using enum
[Rails] How to use enum
[Rails] How to use enum
[Rails] How to upload images using Carrierwave
How to write Rails
How to uninstall Rails
[Rails] How to create a graph using lazy_high_charts
How to create hierarchical category data using ancestry
[rails] How to post images
[Ruby on Rails] How to write enum in Japanese
How to handle uploaded images
[Rails] How to install devise
How to read rails routes
How to use rails join
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
How to handle an instance
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
Rails learning How to implement search function using ActiveModel
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to install a decorator using gem draper
[Rails] How to use Scope
How to authorize using graphql-ruby
How to set environment variables when using Payjp with Rails
[Enum] Let's improve the readability of data by using rails enum
Prevent operations! How to securely update Rails manually using transactions
How to set and describe environment variables using Rails zsh
How to deploy jQuery in your Rails app using Webpacker
[Rails] How to delete images uploaded by carrierwave (using devise)
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
[Rails] How to use flash messages
[rails] How to display db information
How to use Spring Data JDBC
[Rails] How to write in Japanese
[Rails] How to prevent screen transition
[Rails] Various ways to delete data
[How to install Spring Data Jpa]
How to use Ruby on Rails
[Rails] How to add new pages
Rails on Tiles (how to write)
[Rails] How to write exception handling?
[Rails] How to install ImageMagick (RMajick)
[Rails] How to install Font Awesome
[Rails] How to use Active Storage
[Rails] How to implement star rating
How to return Rails API mode to Rails
[Rails] Reflection to db using seeds.rb
How to build CloudStack using Docker
How to use Java enum type
How to install Swiper in Rails
[Rails 6] How to create a dynamic form input screen using cocoon