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
Watch for free

Enter your email below to watch this video

Video thumbnail
Powering Your App with SQLite
Creating tables

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 in an IRB playground to see how it interacts with SQLite, from table creation to schema management. Learn about auto-incremented primary keys, indexing, and migration tracking, setting the foundation for building a full Rails application.

Links

High Performance SQLite Course
Interactive Ruby (IRB) Repository
How to use IRB to explore Ruby

Video Transcript

Before we jump into building our app, let's first get familiar with Active Record so that we can feel comfortable and confident working with the Rails ORM that drives the SQLite engine.

To help with this, I’ve prepared a simple playground in an IRB session, allowing us to explore Active Record's features without setting up a full Rails application. It’s straightforward—we just need to require the Active Record library and a logger. Then, we establish a connection with an SQLite3 in-memory database and configure our logger to output all messages without extra noise.

When working in a playground like this, we can define our schema inside a define block. For example, I'll create a table called "posts", but what I really want to highlight here is the SQL statement that Active Record generates when we create a table.

Understanding Active Record’s Table Creation Process

When we run this, Active Record generates a CREATE TABLE statement for our posts table, including an ID column. Additionally, Active Record automatically creates two other tables:

1. Schema Migrations Table – This table helps Active Record track which migrations have been executed and which are still pending.

2. AR Internal Metadata Table – This table ensures that you don’t accidentally perform destructive actions against your production database.

We’ll dive deeper into how Active Record handles migrations in the next section when we start building the application.

The Three Key Aspects of Our ID Column

Now, let’s focus on the CREATE TABLE statement for our "posts" table and the three key aspects of the ID column:

1. Integer Primary Key as an Alias for ROWID - In SQLite, every table (unless explicitly told otherwise) has a ROWID, an internal index used for tracking rows. By ensuring our ID column is an alias for ROWID, we save storage space and maintain the expected "id" column name without redundant IDs.

2. Auto-Incremented Primary Key for Data Integrity - SQLite automatically increments the ID value for each new row while ensuring deleted IDs are never reused. The official SQLite documentation suggests auto-increment isn’t always necessary, but I’m glad Rails includes it. This extra security ensures that an ID always refers to the same record, even if other records are deleted. While SQLite’s documentation mentions a slight performance overhead, benchmarks show a negligible impact—only around 10 microseconds, making the trade-off well worth the added safety.

3. NOT NULL Constraint for Data Consistency - This ensures that the ID column always has a value. While an integer primary key in SQLite inherently cannot be NULL, having an explicit constraint improves clarity. Rails generates clear, well-structured SQL that explicitly defines table constraints, making database design more transparent.

In the next video, we’ll explore additional fields that a Rails application might need and how to structure them effectively. Let’s dive in!