The RailsNotes Newsletter 🟥 ISSUE #3

🟥 ISSUE #3 (DB seeding, faker gem, upsert_all, and db:seeds:replant)

“an isometric view of a film noir ninja character with a glowing sapling made of rubies, with endless glowing server racks behind him, pixel art style of studio ghibli, moody lighting” (Inspired by my deep dive into database seeding this week)

Advertise with RailsNotes 🟥

This newsletter is looking for advertisers!

If you’re in the Ruby on Rails (or general developer space), learn more at — https://railsnotes.xyz/advertising

Welcome to The RailsNotes Newsletter — Issue #3!

Thank you to all 146 of you for subscribing! 🎉🥳

It’s been a busy week for RailsNotes this week! I published a post on database seeding with the faker gem (see below), which was featured in the excellent Ruby Weekly Newsletter.

I’ve also been diving into ideas for new posts, including lazy-loading with Hotwire, and using the Overmind gem to replace bin/dev. Some of you also replied to last week’s newsletter with some suggestions of your own, which was amazing ❤️

I also have a request for you — Please share this newsletter with an interested friend or co-worker! It would help me out heaps.

One day, I hope that I’ll be able to focus on RailsNotes full/part-time (rather than after hours and during my lunch breaks like I do now 😅), and sharing this newsletter is a great way to help me get there.

Thanks, Harrison.

Featured Article —

This is what I imagine manually seeding your database might look like. Although, you’re probably not as cool as this guy…

I had a lot of fun writing this article, which combines the faker gem with the upsert_all command to rapidly seed 10,000 records into our local dev database.

Initially, this article was only about the faker gem. But, once my seeds started taking 7+ seconds to run, I started wondering — what’s taking so long?! So, I started digging in… and I kept digging…. and digging. And then finally, I started improving things.

The result? 0.8s! We take our seeding time down from 7.5s → 0.8s, for a speedup of 9.4x… 🤯

If you’ve ever had to deal with slow seeding, or you’re curious about what was taking so long, you’ll want to read the full article ⬇️

More articles from around the web

  • Rails 7 adds new options to upsert_all (blog.kiprosh.com) — This article gives a great overview of the upsert_all and insert_all methods that Rails 7 introduced. This article goes much more in-depth than I did, so if you’re interested in learning more (and there is a lot more to learn), this article is a great place to start.

  • Ruby On Rails: way to create different seeds file for environment (stackoverflow.com)This StackOverflow thread was the inspiration for the per-environment database seeding I explained in my article. It’s a simple and elegant solution, and it’s perfect for most situations where you want to split up your seeds.rb file.

  • Add rails db:seed:replant (github.com) — This is the Github issue where DHH suggested adding the rails db:seed:replant command into Rails (see the tip below). This is a quick read (just a few comments on a Github issue), but I really enjoyed the glimpse into DHH’s thinking. Great quote from the issue — This is as much about being clear about your intentions as about outcomes. DHH

A handy tip — re-seed your database with rails db:seed:replant

Rails 6 introduced the db:seed:replant command, but it flew under the radar for most people.

:replant will re-run your database seeds (after TRUNCATE-ing all your database tables to clear them).

In practice, the benefits over db:reset are not huge — :replant runs a bit quicker, since it doesn’t have to delete tables and re-run your migrations. Some though (including DHH), would argue that it’s clearer about what you’re trying to achieve (re-seeding, rather than re-setting).

This tip probably won’t change your life, but I always love to discover little easter eggs like this inside Rails (based on my tweet, a lot of you do too). If you know of any other cool easter eggs like this, reply to this email with them! 

# re-seed your database (without db:reset)
# benefits — 
# - slightly quicker, 
# - clearer intentions, 
# - no db tables deleted

 rails db:seed:replant