Question
Ruby on Rails 4 Data Types Explained for Migrations and Models
Question
Where can I find a clear list of data types that can be used in Ruby on Rails 4?
For example:
text
string
integer
float
date
I keep discovering new ones, and I would like a simple reference list I can use when working with Rails migrations and models.
Short Answer
By the end of this page, you will understand the common data types available in Ruby on Rails 4 migrations, what each type is used for, how Rails maps them to database columns, and how to choose the right type for real applications.
Concept
Rails data types are the column types you use when defining database tables in migrations. They describe what kind of data a column should store, such as short text, long text, whole numbers, dates, or true/false values.
In Rails 4, you usually see these types inside migration files:
create_table :products do |t|
t.string :name
t.text :description
t.integer :stock
t.decimal :price
t.boolean :active
t.date :released_on
t.timestamps
end
Rails uses these migration types as an abstraction layer. That means you write string, integer, or date in Rails, and Rails translates them into the correct database-specific column type for SQLite, PostgreSQL, or MySQL.
This matters because:
- it keeps your code database-friendly
- it helps Active Record understand how to cast values
- it affects validations, querying, sorting, storage size, and precision
- choosing the wrong type can cause bugs or inaccurate data
Common Rails 4 migration data types
Here are the main built-in types you will commonly use:
:string— short text, usually up to 255 characters:text— longer text:integer— whole numbers:float— floating-point numbers:decimal— precise decimal numbers, especially for money
Mental Model
Think of a database table like a spreadsheet with smart columns.
Each column has a label and a rule for what kind of value can go inside:
stringis like a small note fieldtextis like a big paragraph boxintegeris like a counterdecimalis like a calculator that must keep exact digitsbooleanis like a yes/no checkboxdateis like a calendar daydatetimeis like a calendar day plus a clock time
Rails migrations are the instructions that build those columns.
So when you write:
t.string :title
t.integer :views
You are telling Rails:
- create a small text column called
title - create a whole-number column called
views
A helpful way to think about it is: the data type is the shape of the box, and the value must fit the box correctly.
Syntax and Examples
In Rails 4, data types are most often used in migrations.
Basic syntax
class CreateArticles < ActiveRecord::Migration
def change
create_table :articles do |t|
t.string :title
t.text :body
t.integer :views
t.boolean :published
t.date :published_on
t.timestamps
end
end
end
What each line means
t.string :titlestores short textt.text :bodystores longer textt.integer :viewsstores whole numberst.boolean :publishedstorestrueorfalset.date :published_onstores a date without timet.timestampsaddscreated_atandupdated_at
Step by Step Execution
Consider this migration:
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.string :name
t.date :event_date
t.boolean :public
t.timestamps
end
end
end
Step by step
1. Rails reads the migration class
Rails sees a migration named CreateEvents and runs the change method.
2. create_table :events starts a new table
Rails prepares a database table called events.
3. t.string :name
Rails adds a column named name for short text.
Example values:
"Ruby Meetup""Launch Party"
Real World Use Cases
Rails data types appear everywhere in real applications.
User accounts
t.string :email
t.string :encrypted_password
t.boolean :admin, default: false
t.date :birthday
Used for:
- login systems
- profile fields
- account permissions
Blog posts
t.string :title
t.text :body
t.datetime :published_at
t.references :author
Used for:
- content management systems
- publishing workflows
- author relationships
E-commerce products
t.string :name
t.text :description
t.decimal :price, precision: 10, scale: 2
t.integer :stock
Used for:
- product catalogs
- inventory systems
- checkout logic
Scheduling systems
Real Codebase Usage
In real Rails projects, developers do more than just declare types. They choose them carefully to support validation, querying, and maintainability.
Common patterns
Use decimal for money
t.decimal :price, precision: 10, scale: 2
This avoids floating-point precision problems.
Use references for associations
t.references :user
This is common when a model belongs to another model.
Add defaults for booleans
t.boolean :active, default: true
Without defaults, boolean columns can end up nil, which may complicate logic.
Use timestamps for auditing
t.timestamps
Almost every Rails table includes these so developers can track when records were created and updated.
Pair types with validations
Common Mistakes
Beginners often know the type names but are unsure when to use each one.
1. Using float for money
Broken approach:
t.float :price
Why it is a problem:
- floating-point numbers can introduce rounding errors
- money usually needs exact precision
Better:
t.decimal :price, precision: 10, scale: 2
2. Using string for long content
Broken approach:
t.string :article_body
Why it is a problem:
stringis meant for shorter values- long text belongs in
text
Better:
t.text :article_body
3. Storing numbers as strings
Broken approach:
Comparisons
Choosing the right Rails type is easier when you compare similar options.
| Type A | Type B | When to use A | When to use B |
|---|---|---|---|
string | text | Short text like names, titles, emails | Long text like descriptions, comments, article bodies |
integer | float | Whole numbers like counts, ages, stock | Approximate decimal values such as scientific measurements |
float | decimal | Values where tiny precision errors are acceptable | Exact decimal values like prices or balances |
date |
Cheat Sheet
Common Rails 4 migration types
t.string :name
t.text :description
t.integer :count
t.float :rating
t.decimal :price, precision: 10, scale: 2
t.boolean :active
t.date :birthday
t.time :opens_at
t.datetime :published_at
t.timestamp :processed_at
t.binary :file_data
t.references :user
Quick rules
- Use
stringfor short text - Use
textfor long text - Use
integerfor whole numbers - Use
decimalfor money or exact decimal values - Use
floatonly when approximation is acceptable - Use
booleanfor true/false - Use
datefor date only - Use
timefor time only - Use
datetimefor date and time - Use for associations
FAQ
What data types are available in Rails 4 migrations?
Common Rails 4 migration types include string, text, integer, float, decimal, boolean, date, time, datetime, timestamp, binary, and references.
What is the difference between string and text in Rails?
string is for shorter text, such as names or email addresses. text is for longer content, such as article bodies or descriptions.
Should I use float or decimal in Rails?
Use decimal when precision matters, especially for money. Use float only when small rounding differences are acceptable.
Mini Project
Description
Create a simple Rails migration for an online library system. This project helps you practice choosing appropriate database column types for different kinds of data, such as titles, descriptions, prices, publication dates, and availability status.
Goal
Build a books table using sensible Rails 4 data types and understand why each type was chosen.
Requirements
- Create a migration for a
bookstable. - Add a short title field and a long description field.
- Add a price field that stores exact decimal values.
- Add a publication date and an availability flag.
- Add timestamps to track when records are created and updated.
Keep learning
Related questions
Calling a Class Method from an Instance in Ruby
Learn how to call a class method from an instance in Ruby using self.class, with examples, pitfalls, and practical usage patterns.
Calling an Overridden Monkey-Patched Method in Ruby
Learn how to call the original method when monkey patching in Ruby, including alias_method patterns, examples, pitfalls, and practical usage.
Convert a Unix Timestamp to Ruby DateTime
Learn how to convert Unix timestamps to Ruby DateTime and Time objects, with examples, differences, pitfalls, and practical Ruby usage.