High Leverage Rails
Introduction
Introduction to this course
Why use Ruby on Rails
Why use SQLite
Ruby on Rails + SQLite
Powering Your App with SQLite
Creating tables
Timestamps
Column types
Typeof
Ruby types
Creating table introduction
Creating table advanced
Inserting data
Updating data
Upserting data
Reading data
Virtual columns
Enums
Introduction to JSON
Indexing JSON
JSON table functions
Building a Modern Rails Application
Creating a new Rails application
Installing Solid Queue
Installing Solid Cache
Installing Solid Cable
Dockerfile
Application overview
Authentication
Base styles
Registration
Scaffolding posts
Polishing posts
Scaffolding comments
Polishing comments
Polishing models
Polishing controllers
Creating new post
Updating post
Reviewing MVP
Tagging posts
Custom tags
Friendly URLs
Full text search
Deploying & Operating Your App
Backups
Check Litestream locally
Verifying backups
Deployment options
Deploying with Hatchbox
Deployment constraints
Vertical scaling
Database access
Migrations
Locked video

Please purchase the course to watch this video.

Video thumbnail
Powering Your App with SQLite
Column types

Full Course

$
129
$179
USD, one-time fee
This course came at the perfect time. I’ve recently gotten back into Rails after an 18-year hiatus, and this was a perfect refresher and shows just how much you can accomplish with Rails right out of the box.
Garrett Winder
Garrett Winder

Rails hosting made simple

Deploy apps to servers that you own and control.

Move fast and fix things

Application monitoring that helps developers get it done.

Summary

Explore Active Record’s 11 column types and how they map to SQLite’s four main storage classes. Learn how Rails handles binary, boolean, text, numeric, datetime, and JSON data, and discover potential performance improvements in future Rails updates.

Video Transcript

In this video, let's explore how we can define the structure of our tables using the different column types that Active Record provides. As we jump back into the playground, you'll see that I’ve expanded the definition of the posts table to include the 11 different column types that Active Record supports. Let’s walk through each one briefly.

Column Types in Active Record

  • Binary – Stores raw binary data, such as the contents of a PNG file.
  • Boolean – Stores true or false values.
  • String – Stores a small amount of text.
  • Text – Stores larger text content.

A good rule of thumb: If a column only needs to hold a tweet's worth of content (under 256 characters), use string. If it needs more than 256 characters, use text.

Numeric Column Types

  • Integer – Stores whole numbers (e.g., 1, 2, 3, 42).
  • Float & Decimal – Both store real numbers, but with key differences:
    • Float stores numbers imprecisely, which can lead to unexpected behavior in arithmetic operations.
    • Decimal ensures precise and deterministic calculations, making it the preferred choice for financial and mathematical operations.

Date and Time Column Types

  • Date – Stores calendar-based dates (e.g., August 12, 2024).
  • Time – Stores only a time of day (e.g., 11:15 PM).
  • Datetime – Stores both a date and time (e.g., August 12, 2024, at 11:15 PM).

JSON Column Type

  • JSON – Stores structured JSON data.

How Active Record Maps to SQLite Storage Types

Since SQLite does not have a rich type system, it doesn’t store these 11 Active Record types as distinct data types. Instead, SQLite simplifies them into just four storage types:

  • Binary → Stored as a BLOB.
  • Boolean → Stored as an integer (0 or 1), with Active Record enforcing constraints.
  • String & Text → Both stored as TEXT.
  • Integer → Stored as INTEGER.
  • Float & Decimal → Stored as REAL.
  • Datetime & Date → Stored as TEXT, using ISO 8601-formatted strings.
  • JSON → Stored as TEXT.

For those following SQLite advancements, recent updates allow storing JSON documents as blobs instead of text. However, Active Record does not yet support this feature. While this may change in future Rails updates, for most web applications, the performance difference is negligible.

In the next video, we’ll explore how to inspect the database and verify the actual storage types used by SQLite, helping us understand how data is stored on disk.