Skip to main content

Rails 4.2 Introduction

Episode 21 · August 27, 2014

A quick look at the new features of Rails 4.2 in the first beta that was recently released

Rails 4.2


How to install and use Rails 4.2.0.beta1:

gem install rails --pre
rails _4.2.0.beta1_ -v
rails _4.2.0.beta1_ new myapp



Earn a free month

Last week, the first beta of rails 4.2 was released, and it comes with a whole lot of cool new features for rails. The first one is ActiveJob. ActiveJob is built into rails, and you can see it inside of the rails repository in the ActiveJob folder, and basically, this is an API build into rails to talk with the different background worker method. You're probably familiar with delayed job and rescue and Sidekiq and so on, and each of those defines things a little bit differently and all of that, so ActiveJob standardizes all of these, and you can switch between any of those reasonably easily. This is going to be a really great addition because things like sending emails in your rails application need to almost always be sent in the background. You'll be able to use this to send those, and then it will be built into rails so that you always have the option to go ahead and do that, or prepare your application for when you're adding something like rescue or Sidekiq. By default, you can see here that the cue adapter is in line, which means that inside of your rails requests, the email will be sent and it queued in line. What that means is basically as soon as it happens, it will run it, and it's not actually performing in the background because rails is not designed for that, you want to use one of these other tools for this. They provide you that so you can use the API before you actually implement any of these background workers. Easily declaring jobs, you can declare their queues very simply, define a perform method, you can do the work and it accepts a record, and no longer you have to define these queries, so you will pass in the record when you enqueue the job rather than passing in the database id and then looking it up when the job gets around to doing that. That is something that this global id has supported, which is basically a way to serialize your objects and your records when you pass them into your queues. This is really cool, and it supports a whole bunch of adapters already and gives you lots of things. ActiveJob's stats I haven't seen before, but it looks like we can get benchmark and monitoring for our background jobs, which is really cool, so you can have it go and send your data to statsd, and then eventually they're going to add support for other back ends to store those statistics, which is really cool. This is very neat, and a welcome change to rails, because each of your applications may be implementing a different thing like delayed job in one case, and then Sidekiq on another, you can sort of standardize this and get familiar with this, and it doesn't really matter how the background job processing happens. This is neat.

The next thing we have is adequate record, and TenderLoves blog post on this explains sort of how it works, and the improvements that he's made to ActiveRecord that basically give you free speed improvements, so things that are simple like your typical model.find based upon an id or whatever, are going to just get free speed improvements because of his amazing work on ActiveRecord. I definitely recommend checking out his blog post, he goes through and shows you out his benchmarks, and talks about how ActiveRecord works behind the scenes so that you can generate this speed improvements by making these changes in the back end to ActiveRecord, and what he has done with this is really impressive. Definitely give this a read and tweet him a thank you on Twitter because he's awesome. One of the coolest things that they've added to rails is this web console, and what it looks like is when you see this error page, you can see there's an actual interactive console in there, so if you're familiar with running rails console in the terminal, this is just going to be available in your browser, and you can also add this erb tag right into your html and it will add it to the page as you'd like. You can have this at any place where you want it. This is really really useful, you can also go to /console if you need to and play around with code there. This is a way for you to do a little bit more work in your browser, you're probably familiar with this because of better_errors, it's already implemented this, and it looks like just a couple days ago, version 2 drops, I'm definitely excited to check that out. It's cool to see these improvements coming into rails core, and really sort of moving us forward in a direction that's really useful. We are doing all these things by hand but rails is taking a lot of the same concepts and building them into rails directly because: Hey, why not? We might as well have them if you're going to put them in most of your applications.

That's all for this episode, in the future, I'm going to add each of these topics as their own screencasts so we can dive a little bit more in depth with them and play around with them as well. Those hopefully will come out before rails 4.2 drops officially, and you'll be able to hit the ground running. I will see you next episode.


Subscribe to the newsletter

Join 31,353+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.