1 Upgrading to Rails 4.0

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 3.2 in case you haven't and make sure your application still runs as expected before attempting an update to Rails 4.0. A list of things to watch out for when upgrading is available in the Upgrading to Rails guide.

2 Creating a Rails 4.0 application

You should have the 'rails' RubyGem installed
$ rails new myapp
$ cd myapp

2.1 Vendoring Gems

Rails now uses a Gemfile in the application root to determine the gems you require for your application to start. This Gemfile is processed by the Bundler gem, which then installs all your dependencies. It can even install all the dependencies locally to your application so that it doesn't depend on the system gems.

9.1 Notable changes

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

String#to_date now raises ArgumentError: invalid date instead of NoMethodError: undefined method 'div' for nil:NilClass
when given an invalid date. It is now the same as Date.parse, and it accepts more invalid dates than 3.x, such as:

10.2 Deprecations

11 Active Record

11.1 Notable changes

Improve ways to write change migrations, making the old up & down methods no longer necessary.

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 remove, change 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#none implementing 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.

11.2 Deprecations

Deprecated the old-style hash based finder API. This means that methods which previously accepted "finder options" no longer do.

All dynamic methods except for find_by_... and find_by_...! are deprecated. Here's
how you can rewrite the code:

find_all_by_... can be rewritten using where(...).

find_last_by_... can be rewritten using where(...).last.

scoped_by_... can be rewritten using where(...).

find_or_initialize_by_... can be rewritten using where(...).first_or_initialize.

find_or_create_by_... can be rewritten using find_or_create_by(...) or where(...).first_or_create.

find_or_create_by_...! can be rewritten using find_or_create_by!(...) or where(...).first_or_create!.

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.