The RailsNotes Newsletter 🟥 ISSUE #2

🟥 ISSUE #2 (Rails migrations, Indexes, SQL EXPLAIN, and db:migrate:redo)

ruby database

“a server rack with a glowing databases made from crystallised ruby gemstones, digital art, style of studio ghibli, warm lighting, tilt shift lens”

Welcome to The RailsNotes Newsletter — Issue #2!

Thank you to all 125 of you for subscribing! That’s nearly triple last week’s number! 🥳

Truly, I’m touched and extremely grateful for your support ❤️

Fun fact — When I started RailsNotes, I had 0 intention of starting a newsletter. I just planned to write helpful blog posts and share them with the world. As I kept writing, starting a newsletter seemed like the next step, and here we are today!

As always, I’d love to hear from you. If you could reply to this email and let me know 1. how you heard about this newsletter and 2. why you subscribed, that would help me out heaps.

All the best for this week — I’ll be back soon with issue #3.

Thanks, Harrison.

Featured Article —

Databases migrating across the savanna — a “db:migration” 😎

In my latest article, I’ve shared a handy reference for all your rails generate migration needs.

I’m not sure about you, but whenever I generate a new migration in a Rails app, I need to google the correct command (I can never remember if it’s user:references or references:user…. it’s the first one 😅).

This article is the perfect reference guide, and towards the end, I’ve also included some handy tips and commands to make working with Rails migrations easier.

More articles from around the web

  • When is indexing in a model appropriate? (ducktypelabs.com)If you’re anything like me, you have a vague idea about database indexes (ie: they make things go fast 💨). This article is an excellent primer to learn more about indexes in Ruby on Rails apps — It will teach you when an index is appropriate, and how to check that the index you just added actually made a difference.

  •  Using indexes in Rails: Index your associations (tomafro.net)At the time of writing, this article is 14 years old 👵, but don’t let that fool you — this is an excellent, deep look into Rails indexes and their practical implementation. The previous article links to this one, and you can think of this article as a more in-depth version of the first article. This article includes some great explanations of the benefits of indexes by using the EXPLAIN SQL command.

  • Active Record Migrations (guides.rubyonrails.org) — You might have read this article already since it’s one of the official Ruby on Rails guides. I’ve included it here though because I think it’s worth a re-read! I was looking through it when I was writing my Rails Generate Migration article, and I learnt some great new stuff (like automatically adding a column index when you generate a migration with rails g migration CreateModel column:type:index ). I’ve included this as article #3 because it probably won’t blow your mind, but it might be a handy refresher for you.

A handy tip — redo the last couple of db migrations

The rails db:migrate command includes a handy :redo helper to automatically rollback and then re-migrate our database — this is perfect for when you generate a migration and migrate your database, only to realise you need to make a small tweak.

Essentially, it just combines rails db:rollback and rails db:migrate into a single rails db:migrate:redo command (and an optional STEP= flag to control how many migrations to redo).

Here’s an example —

❯ rails db:migrate:redo STEP=1
== 20230709004004 MigrationName: reverting 
== 20230709004004 MigrationName: reverted (0.0018s) 

== 20230709004004 MigrationName: migrating 
== 20230709004004 MigrationName: migrated (0.0000s) 

Make sure to adjust STEP= to match the number of migrations you need to redo.