The first beta of Rails 4.2 was announced last week, and it already looks amazing. I’m really excited to use ActiveJob, Web Console, Adequate Record, and Foreign Key support in my own apps.
But the beauty of Rails is in the details. And if you do a little digging, you’ll find some less advertised features that will totally improve your daily work with Rails.
Easily load config files
OK, I might be biased. But having a built-in way to load config files is going to be awesome.
config_for, new in Rails 4.2, works exactly like you’d expect:
1 2 3 4 5 6 7 8 9
That is, if you call
config_for(:redis), it’ll look for
config/redis.yml in your Rails app, parse it, and returns the right configuration for your
It even lets you put ERB in your yaml:
1 2 3 4 5 6 7 8 9
1 2 3 4
If you configure lots of services in your app, this’ll make your initializers much easier to read.
Bootstrapping your apps
Most Rails apps need you to run some commands before you can try them out. Even mostly-empty Rails apps still need the database to be set up before they’ll boot.
So, Rails, once again going with convention over configuration, created a place specifically for your setup code:
The default is good. But
bin/setup is also the place to put any other code you need to get your app started up.
So now that this is the convention, if you’ve already written a bootstrap script for your app, rename it to
bin/setup, so people using your apps can get started easily.
bin/setup gives you one less decision to make when you generate new Rails apps. And once you get in the habit of running
bin/setup after you
git pull, you’ll never have to remember to run
rake db:setup when you generate a new app again.
Transform your hash values
This is another thing I need often enough that I wish it was built-in to Ruby. When you call
transform_values on a hash, it acts like calling
map on the hash values, and returns a new hash with the original keys associated with the replaced values:
1 2 3
Simple enough. But you’d be surprised how often it comes in handy.
Bonus: More configuration!
Rails 4.2 offers a simple way to set your own global configuration:
1 2 3 4
This works especially well when you combine it with
And there’s more!
It looks like Rails 4.2 is set up to be an amazing release. But it’s the little refinements that make it so great to work in Rails every day. So, if you haven’t yet, take a quick look through the 4.2 Release Notes and see which other helpful improvements you can find.
(And if you do find some cool stuff, don’t keep it to yourself. Share it here, so we can all learn something new!)