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:

config/redis.yml
1
2
3
4
5
6
7
8
9
development:
  host: localhost
  port: 6379
test:
  host: localhost
  port: 6379
production:
  host: redis-production
  port: 6379
1
2
irb(main):001:0> Rails.application.config_for(:redis)
=> {"host"=>"localhost", "port"=>6379}

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 RAILS_ENV.

It even lets you put ERB in your yaml:

config/redis.yml
1
2
3
4
5
6
7
8
9
development:
  host: localhost
  port: <%= ENV['REDIS_PORT'] %>
test:
  host: localhost
  port: <%= ENV['REDIS_PORT'] %>
production:
  host: redis-production
  port: <%= ENV['REDIS_PORT'] %>
1
2
3
4
$ REDIS_PORT=6380 bin/rails c
Loading development environment (Rails 4.2.0.beta1)
irb(main):001:0>  Rails.application.config_for(:redis)
=> {"host"=>"localhost", "port"=>6380}

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: bin/setup.

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
h = {a: 1, b: 2, c: 3}

h.transform_values { |v| v * 2 } # => {a: 2, b: 4, c: 6}

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
Rails.application.config.x.some_configuration_key = "Some Value"

Rails.application.config.x.some_configuration_key # => "Some Value"
Rails.configuration.x.some_configuration_key # => "Some Value"

This works especially well when you combine it with config_for:

1
2
app_config = Rails.application.config_for(:app)
Rails.application.config.x.block_phone_calls = app_config["block_phone_calls"]

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!)

Did you like this post? You should read these:

Finished another Rails tutorial and still don’t know how to start?

Have you slogged through the same guide three times and still can't retain enough to write apps on your own?

In my free 7-part course, you’ll discover the fastest way to learn and remember new Rails ideas, so you can use them when you need them. And you'll learn to use what you already know to build your own Rails project.



Comments