# Adding Decimal Types to Rails Migrations

DevFeed: [Adding Decimal Types to Rails Migrations](<https://devfeed.tech/articles/adding-decimal-types-to-rails-migrations-28246.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/rails/2019/11/18/adding-decimal-types-to-rails-migrations.html>)

Author: Fuzzygroup

Published: 2019-11-18T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [migration](<https://devfeed.tech/topics/migration.md>), [Databases](<https://devfeed.tech/topics/databases.md>)

Tags: [database](<https://devfeed.tech/tags/database.md>), [migration](<https://devfeed.tech/tags/migration.md>), [migrations](<https://devfeed.tech/tags/migrations.md>), [mysql](<https://devfeed.tech/tags/mysql.md>), [precision](<https://devfeed.tech/tags/precision.md>), [rails](<https://devfeed.tech/tags/rails.md>), [scale](<https://devfeed.tech/tags/scale.md>)

## AI overview

A practical guide to adding a decimal column to a Rails migration. It explains why money should not be stored in a floating-point column and shows how precision and scale define the decimal value, including a migration using precision 8 and scale 2.

## Source excerpt

Like so many other things this falls into the category of "I'll do it 100 more times before I die and I can never remember it so may as well blog it". A migration adds a column to a table in a database managed by Rails. In this case I had a table which needed to manage multiple types of data all front ended by an amount method that dynamically figures out why field to fetch back the data from. This gave me a column structure like this: int_val (for integers) float_val (for floats like a measurement) And I needed to add money as a value to this table. Now, yes, I could simply store money in the float_val field but that's a bad practice to start doing because, well, rounding. Even if it really doesn't matter in the app, it is a bad practice to train yourself on. From having done this in the past, I know I need a decimal of the format (something, 2). This is referred to as the 'scale' and the 'precision' (the number of digits to the right of the decimal place). So if we have decimal(8,2), that means you can store: 123,456.78 So your maximum value would be 999,999.99 (just under a million dollars; now say that in a Dr. Evil intonation). Now writing a migration with precision and scale isn't hard but I always want to do this to allow for placing the column in the right place (it makes my tables 'near' and, yes, I'm just that nerdy). Here's my migration: bundle exec rails g migration add_decimal_to_metrics invoke active_record create db/migrate/20191118100314_add_decimal_to_metrics.rb class AddDecimalToMetrics < ActiveRecord::Migration[6.0] def change add_column :metrics, :decimal_val, :decimal, precision: 8, scale: 2, after: :float_val end end References Stack Overflow on Scale and Precision. Mitrev on Migrations