ruby, rails

A List of Short and Powerful Ruby and Rails Tips

List of our favourite Ruby & Rails tips

List of our favourite Ruby & Rails tips

Ruby and Rails gives us so much, it's impossible to know all the cool features we have available. In this post, you'll discover some helpful bits that will make your work easier.

Check out some of our favorites Ruby & Rails tips.

Chain Ruby enumerators with +

Very useful when working with batch queries, or custom enumerators:

Snippet of Ruby Enumerator::Chain

Snippet of a Ruby Enumerator::Chain example

It’s been added on Ruby v2.6. You can also chain enumerators with Enumerator::Chain.new(1..3, [4, 5]), but it’s not as expressive.

Finding all the TODOs on your Rails app

Do you know how many TODOs and FIXMEs are living rent free in your codebase and never got done? (as they usually don’t).

bin/rails notes gives you them all.

List all of your Rails app TODOs and FIXMEs

List all of your Rails app TODOs and FIXMEs with bin/rails notes

Learned this trick from Chad Pytel on our dev channel at thoughtbot.

Add PG enum type with add_enum_value

In Rails 7.1, you can add new values to a PG enum type by calling add_enum_value in a migration.

Snippet of PG enum type with add_enum_value

Add new values to a PG enum type by calling `add_enum_value` in a migration.

Avoid let declarations in your specs

On RSpec, please don’t go crazy with let declarations. They should be used very sparingly.

Even the docs say so:

Avoid let declaration in your specs

Avoid let declaration in your specs

let declarations are like flies: they attract each other. 🪰

You put an extra let today, and tomorrow you’ll get 20 more around it. 🪰🪰

Upgrading Rails tips

ActiveSupport::Notifications - rails.deprecation hook

Want to know what will break on your next Rails upgrade?

Subscribe to the deprecation.rails hook from ActiveSupport::Notifications.

It gives you extra info to help with your upgrade:

  • gem name
  • deprecation horizon
  • the stack trace 🤓
Rails ActiveSupport::Notification deprecation.rails

Snippet of how to subscribe to the `deprecation.rails` hook from ActiveSupport::Notifications

Rails 7 - filter_parameter_logging changes

When upgrading from Rails 6 to 7, you’ll notice there are some changes in the config/initializers/filter_parameter_logging.rb file. This was how it looked like before running rails app:update:

# config/initializers/filter_parameter_logging.rb

Rails.application.config.filter_parameters += [:password]

And this is how it looks after running the Rails upgrade command:

# config/initializers/filter_parameter_logging.rb

Rails.application.config.filter_parameters += [
  :passw, :secret, :token, :_key, # other params
]

It includes new params, which is great. But did you notice how password changed to passw?

At first, I thought it was a typo. Ha, silly me. As this comment explains, these filter parameters are checked by a regular expression, so “password” matches “passw” as well.

Not exactly a tip but it’s an interesting finding that I hope saves you some time when you upgrade your Rails 6 app.

Extra: git ammend

Ran a git commit but forgot to add one file?

git commit --amend --all adds all unstaged changes to the last commit 😙

git commit --amend --all command example

Use it like this or add it to your dotfiles

Learned this trick recently by watching one episode from GracefulDev 🤓


Which one of these tips surprised you the most? Do you already use any of these tips? Let us know, we’d love to hear more.