Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's New in Rails 4.2

What's New in Rails 4.2

A brief overview of new features in Ruby on Rails 4.2

Presented at Austin on Rails on October 28, 2014

Anthony Lewis

October 28, 2014

More Decks by Anthony Lewis

Other Decks in Programming


  1. Rails 4.2 • Active Job • Action Mailer • Global

    ID • Adequate Record • Web Console • Other Changes • Installation • Release Notes
  2. Active Job • Framework for background processing using Resque, Delayed

    Job, Sidekiq, etc. • Switch between background runners without having to rewrite jobs • If no adapter is specified, jobs run immediately
  3. Configuration • Add your preferred background processing library to your

    application’s Gemfile: • Update Rails configuration: gem 'sidekiq' config.active_job.queue_adapter = :sidekiq
  4. Creating Jobs • Use the Rails generator to create a

    new job: $ bin/rail g job process_video create app/jobs/process_video_job.rb
  5. Job Source • The newly created job: class ProcessVideoJob <

    ActiveJob::Base queue_as :default def perform(*args) # Do something later end end
  6. Enqueue Job • Enqueue with or without a time: #

    Run next ProcessVideo.perform_later(@video) # Run tomorrow ProcessVideo.set(wait: 1.day) .perform_later(@video)
  7. Error Handling • Catch exceptions with a block: class ProcessVideoJob

    < ActiveJob::Base ... rescue_from(Exception) do |e| # do something with exception end end
  8. Action Mailer • ActionMailer now uses ActiveJob to send email

    • Call deliver_now to send a message inline # Send inline UserMailer.success_email(@user) .deliver_now
  9. Action Mailer • Call deliver_later to enqueue delivery • Accepts

    :wait, :wait_until, and :queue options # Enqueue and send later UserMailer.success_email(@user) .deliver_later
  10. Global ID • A unique identifier for a model instance

    • ActiveJob uses Global ID to automatically serialize and deserialize objects irb> video.to_global_id.to_s => "gid://appname/Video/1"
  11. Adequate Record • Optimizations to Active Record that make the

    find, find_by_*, and find_by methods up to 2x faster • Caches the static parts of the SQL query User.find(1) # Caches the static part: # SELECT * FROM users WHERE id = ? User.find(2) # Uses the cache, executes faster
  12. Web Console • Debugging tool for your Rails app •

    Automatically opens a Rails console on exceptions • Can also be opened with the console view helper <%= console %>
  13. Other Changes • Previously deprecated functionality has been removed •

    Check for deprecation warnings before updating • The respond_with / class-level respond_to methods have been moved to the responders gem • The default host for rails server is now localhost instead of • If you dev in a virtual machine this will bite you
  14. Other Changes • The default log level for production is

    now :debug, just like development and test • The HTML sanitizer has been replaced with a new version. • Some sanitized output might change • Foreign key support (finally) • And more…
  15. Installation # In your terminal: $ gem install rails --pre

    # In your Gemfile: gem 'rails', '~> 4.2.0.beta2'