Rails 3: New features & Changes

Michel Golffed
November 18, 2010 | 2 min read

In this post I’ll show you some basic examples about the new features and changes of the web framework Rails 3, for this I’ll use two models: User and Task, having a relationship between them where a User has_many :tasks.

ActiveRecord finder methods

Let’s start by finding the five most recently created users:
Rails 2

What’s new about this method call in rails 3?
Rails 3 looks at the hash of options that is being passed to find(:all, : order, and :limit) and replace each item in the hash with an equivalent method.


New API methods:


where, having, select, group, order, limit, offset, joins, includes, …
So, in Rails 3 this becomes:
Let’s continue with this second example:
Rails 2:
Rails 3:
In this example the “where” method substitutes the :conditions parameter.
Named Scopes
Changes to named_scopes in Rails 3:
User model in rails 2 with two named scopes:
The same in rails 3 would be:
Note that the method that we use to define as named_scope has become to just “scope”. Also, we no longer pass the conditions as a hash but, as with find, we use methods.

Another new feature is the ability to build up scopes. If we want to create a scope called “recent”, that will return the most recently created active south american users ordered by their creation date, we can do so by reusing the two scopes we already have.
Chaining together the two scopes we already have and add an order method to create the new scope, is all we need to do.
Here is the result:

ActiveRecord Validation

Separate validations in Rails 2

All validations together for a field in Rails 3
Hope you enjoyed it, some other features of Rails 3 will be published on upcoming posts.

Get our stories delivered to your inbox weekly.