An Overview of the Rails Data Types

Jacob Knopf
3 min readJul 4, 2020

In my previous post I discussed Rails migrations, and some important concepts to be aware of. I also mentioned how the documentation is great for answering most questions you could have about any topic. The only area I think it’s lacking is giving a bit more information about each of the Rails data types, and when to use them. I mean honestly what’s the exact difference between a :float and :decimal, or :text and :string? Read on and I can explain don’t panic!

Here is a list of all the common data types Rails has to offer:

  • :primary_key
  • :string
  • :text
  • :integer
  • :bigint
  • :float
  • :decimal
  • :datetime
  • :time
  • :date
  • :binary
  • :boolean

I’m not including :references in this list because it’s not really an actual data type. Now let’s get into what each of these are used for, and their similarities/differences. Sometimes it’s not exactly clear at first and it’s easy to mistake which one would be best suited for your database.

:primary_key — this data type is a kind of placeholder that Rails translates as a unique key to identify each row in your table.

:string — used for short text fields, think “name” or “title” attributes, and has to be less than 255 characters.

:text — used for longer text fields, think “comment” or “review” attributes, and has a character limit of approximately 30,000 characters.

:integer — this type is used specifically for whole numbers ONLY, and can store numbers up to 2.1 billion.

:bigint — similar to :integer, with the difference being that it can store whole numbers up to approximately 20 digits long.

:float — used for decimal numbers with fixed precision.

:decimal — also used for decimal numbers, but use this type if you need to make specific calculations (precision is NOT fixed).

:datetime — this data type is also known as :timestamp in Rails and they mean the same thing, used to store the date and the time.

:time — used to store a time ONLY (hours, minutes, seconds).

:date — used to store a date ONLY (year, month, day).

:binary — this type is used for storing data like images, movies, or audio files in their original raw format.

:boolean — used for true/false values, think of things with two states (“not finished” and “complete,” or “on” and “off”).

I hope this helped clear up any confusion surrounding some of the general Rails data types. I know when I first started learning to code at Flatiron I was pretty confused about when to use:integer, :float, or :decimal . If you have any further questions about these data types, or other ones like :array which you can use with a PostgreSQL database, I would suggest going to Stack Overflow. There are quite a few helpful posts about this exact subject thankfully. Have a safe and healthy holiday weekend everyone!

--

--

Jacob Knopf

QA Lead | Content Designer | UX Researcher | Developer Evangelist