Folks! Great news! Just in time for the opening of RailsConf, the first release candidate of Rails 4.0 was managed to push out. Here are the nice guides to upgrade to Rails 4.0 http://rubysource.com/get-your-app-ready-for-rails-4/
This post is about the summary of the new features that Rails 4.0 offers, as well as the changes that may affect your current applications.
Highlights in Rails 4.0:
- Ruby 2.0 preferred; 1.9.3+ required
- Strong Parameters
- Russian Doll Caching
Rails 4 will only support Ruby 1.9.3+ and most compatible with Ruby 2.0. Get ready to upgrade if you have not done so to experience Rails 4.
No more vendor/plugins
The vendor/plugins directory has been removed in Rails 4. Instead, you should use Bundler with a path or Git dependencies. If you still have any code in vendor/plugins, you have 2 options:
- Move it to a gem. Most plugins should already have a gem version. If not, you can reference it from your Gemfile via the
- Move it to
lib/your_pluginand require it from an initializer on
In new Rails applications, the
threadsafe! option will be enabled by default in production mode. The way to turn it off will be by setting
This shouldn’t be much of a problem, unless the app (or one of its gems) is depending too much on thread safety.
A lot of controversy was generated around the mass assignment protection. Since then, various different approaches were explored.
Rails 3.2 uses
attr_protected on the models.
Rails 4 takes a whole different point of view, making the controller responsible on what attributes are assigned to the model.
An advantage of this approach is that it allows us to filter different parameters in a user facing action than in an admin area.
Also, different actions could permit different sets of allowed attributes. Having the controller in charge of what the model receives makes sense.
Turbolinks is kind of like the brother of pjax. It uses
pushState when available, triggering an XHR request and replacing the
<body> content. This has 3 main benefits:
- Doesn’t download CSS/JS twice.
- Doesn’t recompile.
- Doesn’t reevaluate.
Rails 4 comes with Turbolinks enabled by default. But this can lead to some problems if you’re using too much code in the
domready event. TurboLinks declares 4 new events:
page:fetchstarting to fetch the target page (only called if loading fresh, not from cache).
page:loadfetched page is being retrieved fresh from the server.
page:restorefetched page is being retrieved from the 10-slot client-side cache.
page:changepage has changed to the newly fetched version.
To use it now, just install the gem and add
As action and page caching are removed from Rails 4, there is introducing a new way to store caches. It’s via Key based expiration.
The main idea is to append the
updated_at attribute to the cache key. When a record changes, it’s cache key changes and it will fetch a fresh version of it.
This approach generates a lot of cache garbage, so it works better with memcache-like stores where it removes oldest keys first when it’s running out of space.
Also, for views it generates a MD5 checksum of the file on the cache key. If anything in the file changes, the cache key changes and it fetches everything again.
ActiveModel::ForbiddenAttributesProtection, a simple module to protect attributes from mass assignment when non-permitted attributes are passed.
ActiveModel::Model, a mixin to make Ruby objects work with Action Pack out of box.
- Replace deprecated
- Optimize ActiveSupport::Cache::Entry to reduce memory and processing overhead.
- Inflections can now be defined per locale.
pluralizeaccept locale as an extra argument.
Object#trywill now return nil instead of raise a NoMethodError if the receiving object does not implement the method, but you can still get the old behavior by using the new
- Change the stylesheet of exception pages for development mode. Additionally display also the line of code and fragment that raised the exception in all exceptions pages.
- The methods
remove_columnare now reversible, as long as the necessary information is given. The method
remove_columnused to accept multiple column names; instead use
remove_columns(which is not revertible). The method
change_tableis also reversible, as long as its block doesn’t call
- New method
reversiblemakes it possible to specify code to be run when migrating up or down. See the Guide on Migration
- New method
revertwill revert a whole migration or the given block. If migrating down, the given migration / block is run normally. See the Guide on Migration
- Adds PostgreSQL array type support. Any datatype can be used to create an array column, with full migration and schema dumper support.
Relation#loadto explicitly load the record and return
Model.allnow returns an
ActiveRecord::Relation, rather than an array of records. Use
Relation#to_aif you really want an array. In some specific cases, this may cause breakage when upgrading.
ActiveRecord::Migration.check_pending!that raises an error if migrations are pending.
- Added custom coders support for
ActiveRecord::Store. Now you can set your custom coder like this:
store :settings, accessors: [ :color, :homepage ], coder: JSON
mysql2connections will set
SQL_MODE=STRICT_ALL_TABLESby default to avoid silent data loss. This can be disabled by specifying
strict: falsein your
- Remove IdentityMap.
- Remove automatic execution of EXPLAIN queries. The option
active_record.auto_explain_threshold_in_secondsis no longer used and should be removed.
ActiveRecord::Relation#noneimplementing the null object pattern for the Relation class.
create_join_tablemigration helper to create HABTM join tables.
- Allows PostgreSQL hstore records to be created.
Here are some sites that are using Rails 4:
To better understanding and applying Rails 4.0 in your project, do refer to the course video teaching Rails 4.0 with Zombie in codeschool.com or check out the list of commits in the main Rails repository on GitHub.