What Is New in Ruby on Rails 4.2
| Category | Highlights |
|---|---|
| New Features | Active Job framework, deliver_later for Action Mailer, Web Console, foreign-key migration helpers, config.x custom namespace, Rails.application.config_for, bin/setup script |
| Improvements | Adequate Record prepared-statement caching, automatic ETag digest inclusion, gzip asset serving, URL segment auto-escaping, always_permitted_parameters default |
| Breaking Changes | render "foo/bar" now treats the string as a template, class-level respond_with moved to responders gem, rails server defaults to localhost, changed :status symbols, render nothing: true padding removed, filter method names deprecated |
| Deprecations | respond_with / class-level respond_to, only_path option on *_path helpers, assert_tag family, deliver / deliver! in mailers, *_path helpers in mailers, swallowing errors in after_commit/after_rollback |
How does Rails 4.2 simplify background job processing?
Rails 4.2 introduces Active Job, a single API that abstracts over Resque, Sidekiq, Delayed Job and other queuing back-ends.
Jobs are defined by subclassing ActiveJob::Base and can receive Active Record objects directly because Global ID serialises them as URIs.
class TrashableCleanupJob < ActiveJob::Base
def perform(trashable, depth)
trashable.cleanup(depth)
end
end
Action Mailer now ships with deliver_later, which enqueues the email as an Active Job instead of sending it synchronously.
UserMailer.welcome_email(user).deliver_later
In practice this means you can add background processing to an existing app with a single gem change and no code rewrite.
What database performance and schema enhancements arrive in Rails 4.2?
Rails 4.2 adds Adequate Record, which caches common find and find_by queries as prepared statements, cutting the SQL generation overhead by up to 2×.
Post.find(1) # first call creates the prepared statement
Post.find(2) # subsequent call reuses it
The migration DSL now supports foreign keys for MySQL, MySQL2 and PostgreSQL, and these keys are dumped into schema.rb so they survive schema loading.
add_foreign_key :articles, :authors
remove_foreign_key :accounts, column: :owner_id
This matters if you rely on referential integrity at the database level or need the extra performance boost from prepared statements.
Which developer productivity tools are baked into new Rails 4.2 apps?
New Rails 4.2 applications include the Web Console gem, giving you an interactive Ruby console on every error page and a console helper for on-demand debugging.
The config.x namespace lets you store arbitrary configuration without polluting the main config namespace.
# config/environments/production.rb
config.x.payment_processing.schedule = :daily
config.x.payment_processing.retries = 3
Rails 4.2 also adds Rails.application.config_for for loading environment-specific YAML files, and a bin/setup script that standardises project bootstrapping.
What are the key breaking changes you must address when upgrading to Rails 4.2?
Watch out for these upgrade-time gotchas:
- render with a string argument now renders a template; use
render file: "foo/bar"if you need to render a raw file. - respond_with and class-level respond_to have been removed; add
gem "responders", "~> 2.0"to continue using them. - rails server now binds to
localhostby default; start withrails server -b 0.0.0.0to expose it on the network. - :status symbols have changed -
:request_entity_too_largeis now:payload_too_large, etc. - render nothing: true no longer adds a space; prefer
head :okfor empty responses. - filter callbacks are deprecated in favour of
*_actionequivalents (e.g.,before_actioninstead ofbefore_filter).
Most teams can upgrade by running the test suite, fixing the above items, and adding the responders gem if needed.
Frequently Asked Questions
Do I need to add a new gem to keep using respond_with in Rails 4.2?
Yes you must add gem "responders", "~> 2.0" to your Gemfile.
How do I add a foreign key in a Rails 4.2 migration?
Use add_foreign_key :articles, :authors inside the change method.
What method replaces deliver in Action Mailer?
Use deliver_now for immediate delivery or deliver_later for asynchronous delivery.
How can I open a console on an exception page?
The Web Console gem is included by default and automatically appears on error pages.
Is render nothing: true still supported?
No, render nothing: true no longer adds a space; use head :ok instead.
Can I still use before_filter in Rails 4.2?
before_filter works but is deprecated; replace it with before_action.