[Ruby On Rails] How to update the calculated result to an integer type column using update_column

It is a memorandum.

About update and update_column

If you want to update the record when updating the information in the table, use the update method. However, if you want to update only a specific column, you can't use update. So use update_column.

Example of use

As a premise, it is assumed that there is an integer type column called users table: post_count that counts the number of posts.

Each time the user posts, the number of posts (= post_count) is added as follows.

    sum = current_user.post_count.to_i + 1
    current_user.update_column(:post_count, sum.to_i)

To_i is added so that addition can be performed firmly even when the initial value is nil. If the column is nil and it is executed without to_i, the following error will occur.

undefined method `+' for nil:NilClass スクリーンショット 2021-01-08 19.27.43.png

By doing to_i, nil can be recognized as the number 0 and can be calculated.

Reference article

https://qiita.com/lemtosh469/items/371544fa4fd3c333adf1 https://teratail.com/questions/19963

Recommended Posts

[Ruby On Rails] How to update the calculated result to an integer type column using update_column
[Ruby on Rails] How to change the column name
[Ruby On Rails] How to search the contents of params using include?
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
How to make an application with ruby on rails (assuming that the environment has been built)
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to solve the local environment construction of Ruby on Rails (MAC)!
[Rails] How to create a table, add a column, and change the column type
How to debug the processing in the Ruby on Rails model only on the console
[Ruby on Rails] How to log in with only your name and password using the gem devise
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
[Ruby on Rails] How to display error messages
How to add / remove Ruby on Rails columns
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Ruby on Rails] How to make the link destination part of the specified id
[Ruby on Rails] Change the update date and creation date to your favorite notation
How to resolve errors that occur in the "Ruby on Rails" integration test
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
How to implement image posting function using Active Storage in Ruby on Rails
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
Method summary to update multiple columns [Ruby on Rails]
[Ruby on Rails] How to write enum in Japanese
[Ruby on Rails] Change URL id to column name
[Rails] How to change the column name of the table
[Ruby On Rails] How to reset DB in Heroku
(Ruby on Rails6) How to create models and tables
[Rails] How to display an image in the view
Try using the query attribute of Ruby on Rails
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Ruby On Rails / HTML] How to display the cursor (caret) at an appropriate place when displaying a web page
[Ruby On Rails] How to search and save the data of the parent table from the child table
Add an icon to the header link using Rails fontawesome
Prevent operations! How to securely update Rails manually using transactions
How to retrieve the hash value in an array in Ruby
How to display a graph in Ruby on Rails (LazyHighChart)
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
How to install JDK 8 on Windows without using the installer
Use WHERE to retrieve the result calculated using the Mysql syntax
[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
How to run React and Rails on the same server
[Rails] How to put a crown mark on the ranking function
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Ruby on Rails] Use the resources method to automatically create routes.
Things to remember and concepts in the Ruby on Rails tutorial
[Ruby on Rails] How to avoid creating unnecessary routes for devise
(Ruby on Rails6) Create a function to edit the posted content
[Ruby on Rails] Pass the parameters divided by date_select to FormObject.
[Rails 5] How to display the password change screen when using devise
How to deploy jQuery on Rails
[Java] How to update Java on Windows
How to deploy Bootstrap on Rails
Rails on Tiles (how to write)
How to place geckodriver (Selenium WebDriver) on the path using Maven command
[Android] How to turn the Notification panel on and off using StatusBarManager
[Swing] How to display an arbitrary name on the menu bar on Mac
How to implement gem "summer note" in wysiwyg editor in Ruby on Rails
How to build an environment for any version of Ruby using rbenv