Contact Us

Ruby on Rails 4 – first release candidate released

Ruby on Rails, Technologies | June 10, 2013

Folks! Great news! Just in time for the opening of RailsConf, the first release candidate of Rails 4.0 was managed to be pushed 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.

  • Ruby 2.0 preferred; 1.9.3+ required
  • Strong Parameters
  • Turbolinks
  • Russian Doll Caching

Ruby >=1.9.3

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 :git or :path options.
  • Move it to lib/your_plugin and require it from an initializer on config/initializers.
Threadsafe by default

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 config.cache_classes andconfig.eager_load to false.

This shouldn’t be much of a problem, unless the app (or one of its gems) is depending too much on thread safety.

Strong parameters

A lot of controversy was generated around the mass assignment protection. Since then, various different approaches were explored.

Rails 3.2 uses attr_accessible and 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

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:fetch starting to fetch the target page (only called if loading fresh, not from cache).
  • page:load fetched page is being retrieved fresh from the server.
  • page:restore fetched page is being retrieved from the 10-slot client-side cache.
  • page:change page has changed to the newly fetched version.

You’ll have to check your javascript and fix it where necessary.

To use it now, just install the gem and add //= require turbolinks to your main javascript file (usually application.js)

Russian dolls

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.

Active model

  • Add ActiveModel::ForbiddenAttributesProtection, a simple module to protect attributes from mass assignment when non-permitted attributes are passed.
  • Added ActiveModel::Model, a mixin to make Ruby objects work with Action Pack out of box.

Active support

  • Replace deprecated memcache-client gem with dalli in ActiveSupport::Cache::MemCacheStore.
  • Optimize ActiveSupport::Cache::Entry to reduce memory and processing overhead.
  • Inflections can now be defined per locale. singularize and pluralize accept locale as an extra argument.
  • Object#try will 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 Object#try!.

Action pack

  • 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.

Active record

  • The methods drop_table and remove_column are now reversible, as long as the necessary information is given. The method remove_column used to accept multiple column names; instead use remove_columns (which is not revertible). The method change_table is also reversible, as long as its block doesn’t call removechange or change_default
  • New method reversible makes it possible to specify code to be run when migrating up or down. See the Guide on Migration
  • New method revert will 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.
  • Add Relation#load to explicitly load the record and return self.
  • Model.all now returns an ActiveRecord::Relation, rather than an array of records. Use Relation#to_a if you really want an array. In some specific cases, this may cause breakage when upgrading.
  • Added 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
  • mysql and mysql2 connections will set SQL_MODE=STRICT_ALL_TABLES by default to avoid silent data loss. This can be disabled by specifying strict: false in your database.yml.
  • Remove IdentityMap.
  • Remove automatic execution of EXPLAIN queries. The option active_record.auto_explain_threshold_in_seconds is no longer used and should be removed.
  • Adds ActiveRecord::NullRelation and ActiveRecord::Relation#noneimplementing the null object pattern for the Relation class.
  • Added create_join_table migration 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.
Reference: http://net.tutsplus.com/tutorials/ruby/digging-into-rails-4/

http://rubysource.com/get-your-app-ready-for-rails-4/

http://edgeguides.rubyonrails.org/4_0_release_notes.html