Ruby on Rails 4.2 Release Notes

Highlights in Rails 4.2:

Active Job

Asynchronous mails

Adequate Record

Web Console

Foreign key support

These release notes cover only the major changes. To learn about other
features, bug fixes, and changes, please refer to the changelogs or check out
the list of commits in
the main Rails repository on GitHub.

1 Upgrading to Rails 4.2

If you're upgrading an existing application, it's a great idea to have good test
coverage before going in. You should also first upgrade to Rails 4.1 in case you
haven't and make sure your application still runs as expected before attempting
to upgrade to Rails 4.2. A list of things to watch out for when upgrading is
available in the guide Upgrading Ruby on
Rails.

2 Major Features

2.1 Active Job

Active Job is a new framework in Rails 4.2. It is a common interface on top of
queuing systems like Resque, Delayed
Job,
Sidekiq, and more.

Jobs written with the Active Job API run on any of the supported queues thanks
to their respective adapters. Active Job comes pre-configured with an inline
runner that executes jobs right away.

Jobs often need to take Active Record objects as arguments. Active Job passes
object references as URIs (uniform resource identifiers) instead of marshaling
the object itself. The new Global ID
library builds URIs and looks up the objects they reference. Passing Active
Record objects as job arguments just works by using Global ID internally.

For example, if trashable is an Active Record object, then this job runs
just fine with no serialization involved:

2.2 Asynchronous Mails

Building on top of Active Job, Action Mailer now comes with a deliver_later
method that sends emails via the queue, so it doesn't block the controller or
model if the queue is asynchronous (the default inline queue blocks).

Sending emails right away is still possible with deliver_now.

2.3 Adequate Record

Adequate Record is a set of performance improvements in Active Record that makes
common find and find_by calls and some association queries up to 2x faster.

It works by caching common SQL queries as prepared statements and reusing them
on similar calls, skipping most of the query-generation work on subsequent
calls. For more details, please refer to Aaron Patterson's blog
post.

Active Record will automatically take advantage of this feature on
supported operations without any user involvement or code changes. Here are
some examples of supported operations:

It's important to highlight that, as the examples above suggest, the prepared
statements do not cache the values passed in the method calls; rather, they
have placeholders for them.

Caching is not used in the following scenarios:

The model has a default scope

The model uses single table inheritance

find with a list of ids, e.g.:

# not cached
Post.find(1, 2, 3)
Post.find([1,2])

find_by with SQL fragments:

Post.find_by('published_at < ?', 2.weeks.ago)

2.4 Web Console

New applications generated with Rails 4.2 now come with the Web
Console gem by default. Web Console adds
an interactive Ruby console on every error page and provides a console view
and controller helpers.

The interactive console on error pages lets you execute code in the context of
the place where the exception originated. The console helper, if called
anywhere in a view or controller, launches an interactive console with the final
context, once rendering has completed.

2.5 Foreign Key Support

The migration DSL now supports adding and removing foreign keys. They are dumped
to schema.rb as well. At this time, only the mysql, mysql2 and postgresql
adapters support foreign keys.

3 Incompatibilities

Previously deprecated functionality has been removed. Please refer to the
individual components for new deprecations in this release.

The following changes may require immediate action upon upgrade.

3.1 render with a String Argument

Previously, calling render "foo/bar" in a controller action was equivalent to
render file: "foo/bar". In Rails 4.2, this has been changed to mean
render template: "foo/bar" instead. If you need to render a file, please
change your code to use the explicit form (render file: "foo/bar") instead.

3.2 respond_with / Class-Level respond_to

respond_with and the corresponding class-level respond_to have been moved
to the responders gem. Add
gem 'responders', '~> 2.0' to your Gemfile to use it:

However, with this change you will no longer be able to access the Rails
server from a different machine, for example if your development environment
is in a virtual machine and you would like to access it from the host machine.
In such cases, please start the server with rails server -b 0.0.0.0 to
restore the old behavior.

If you do this, be sure to configure your firewall properly such that only
trusted machines on your network can access your development server.

3.4 HTML Sanitizer

The HTML sanitizer has been replaced with a new, more robust, implementation
built upon Loofah and
Nokogiri. The new sanitizer is
more secure and its sanitization is more powerful and flexible.

Due to the new algorithm, the sanitized output may be different for certain
pathological inputs.

If you have a particular need for the exact output of the old sanitizer, you
can add the rails-deprecated_sanitizer
gem to the Gemfile, to have the old behavior. The gem does not issue
deprecation warnings because it is opt-in.

rails-deprecated_sanitizer will be supported for Rails 4.2 only; it will not
be maintained for Rails 5.0.

See this blog post
for more details on the changes in the new sanitizer.

3.5 assert_select

assert_select is now based on Nokogiri.
As a result, some previously-valid selectors are now unsupported. If your
application is using any of these spellings, you will need to update them:

Values in attribute selectors may need to be quoted if they contain
non-alphanumeric characters.

5 Action Pack

5.1 Removals

respond_with and the class-level respond_to have been removed from Rails and
moved to the responders gem (version 2.0). Add gem 'responders', '~> 2.0'
to your Gemfile to continue using these features.
(Pull Request,
More Details)

If your application currently depends on these methods, you should use the
replacement *_action methods instead. These methods will be deprecated in
the future and will eventually be removed from Rails.

When the Rails server is set to serve static assets, gzip assets will now be
served if the client supports it and a pre-generated gzip file (.gz) is on disk.
By default the asset pipeline generates .gz files for all compressible assets.
Serving gzip files minimizes data transfer and speeds up asset requests. Always
use a CDN if you are
serving assets from your Rails server in production.
(Pull Request)

When calling the process helpers in an integration test the path needs to have
a leading slash. Previously you could omit it but that was a byproduct of the
implementation and not an intentional feature, e.g.:

6.2 Notable changes

The form helpers no longer generate a <div> element with inline CSS around
the hidden fields.
(Pull Request)

Introduced a #{partial_name}_iteration special local variable for use with
partials that are rendered with a collection. It provides access to the
current state of the iteration via the index, size, first? and
last? methods.
(Pull Request)

Removed unused :timestamp type. Transparently alias it to :datetime
in all cases. Fixes inconsistencies when column types are sent outside of
Active Record, such as for XML serialization.
(Pull Request)

8.2 Deprecations

Deprecated broken support for automatic detection of counter caches on
has_many :through associations. You should instead manually specify the
counter cache on the has_many and belongs_to associations for the
through records.
(Pull Request)

Deprecated half-baked support for PostgreSQL range values with excluding
beginnings. We currently map PostgreSQL ranges to Ruby ranges. This conversion
is not fully possible because Ruby ranges do not support excluded beginnings.

The current solution of incrementing the beginning is not correct
and is now deprecated. For subtypes where we don't know how to increment
(e.g. succ is not defined) it will raise an ArgumentError for ranges
with excluding beginnings.
(Commit)

Deprecated calling DatabaseTasks.load_schema without a connection. Use
DatabaseTasks.load_schema_current instead.
(Commit)

Deprecated sanitize_sql_hash_for_conditions without replacement. Using a
Relation for performing queries and updates is the preferred API.
(Commit)

Deprecated add_timestamps and t.timestamps without passing the :null
option. The default of null: true will change in Rails 5 to null: false.
(Pull Request)

Deprecated Reflection#source_macro without replacement as it is no longer
needed in Active Record.
(Pull Request)

Deprecated returning nil from column_for_attribute when no column
exists. It will return a null object in Rails 5.0.
(Pull Request)

Deprecated using .joins, .preload and .eager_load with associations
that depend on the instance state (i.e. those defined with a scope that
takes an argument) without replacement.
(Commit)

8.3 Notable changes

SchemaDumper uses force: :cascade on create_table. This makes it
possible to reload a schema when foreign keys are in place.

Added a :required option to singular associations, which defines a
presence validation on the association.
(Pull Request)

ActiveRecord::Dirty now detects in-place changes to mutable values.
Serialized attributes on Active Record models are no longer saved when
unchanged. This also works with other types such as string columns and json
columns on PostgreSQL.
(Pull Requests 1,
2,
3)

Introduced the db:purge Rake task to empty the database for the
current environment.
(Commit)

Introduced ActiveRecord::Base#validate! that raises
ActiveRecord::RecordInvalid if the record is invalid.
(Pull Request)

touch now accepts multiple attributes to be touched at once.
(Pull Request)

The PostgreSQL adapter now supports the jsonb datatype in PostgreSQL 9.4+.
(Pull Request)

The PostgreSQL and SQLite adapters no longer add a default limit of 255
characters on string columns.
(Pull Request)

Added support for the citext column type in the PostgreSQL adapter.
(Pull Request)

Added support for user-created range types in the PostgreSQL adapter.
(Commit)

sqlite3:///some/path now resolves to the absolute system path
/some/path. For relative paths, use sqlite3:some/path instead.
(Previously, sqlite3:///some/path resolved to the relative path
some/path. This behavior was deprecated on Rails 4.1).
(Pull Request)

Deprecated ActiveSupport::SafeBuffer#prepend! as
ActiveSupport::SafeBuffer#prepend now performs the same function.
(Pull Request)

10.3 Notable changes

Introduced a new configuration option active_support.test_order for
specifying the order test cases are executed. This option currently defaults
to :sorted but will be changed to :random in Rails 5.0.
(Commit)

Object#try and Object#try! can now be used without an explicit receiver in the block.
(Commit,
Pull Request)

The travel_to test helper now truncates the usec component to 0.
(Commit)

Feedback

You may also find incomplete content, or stuff that is not up to date.
Please do add any missing documentation for master. Make sure to check
Edge Guides first to verify
if the issues are already fixed or not on the master branch.
Check the Ruby on Rails Guides Guidelines
for style and conventions.

If for whatever reason you spot something to fix but cannot patch it yourself, please
open an issue.