"New Features in Rails 3"

We’re all about following the newest technology in web development. Rails 3 is coming soon, and it promises to be a significant upgrade. With the 4th beta version pushed recently, and a release candidate right around the corner, I thought now would be a good time to go over my two favorite new features.

All New Router


The router has been completely re-written and borrows heavily from the Merb router, but has a more developed DSL.


Regular Routes


Regular routes are more concise, but I’m not sure how I feel about the “controller#action” syntax yet…


<script src=”http://gist.github.com/448079.js?file=gistfile1.builder”></script&gt;


Named Routes


Very similar to regular routes, just with an extra :as variable at the end.


<script src=”http://gist.github.com/448079.js?file=gistfile2.builder”></script&gt;


Restful Routes


I think restful routes get the biggest improvement. The new syntax is MUCH easier to read than in Rails 2.


<script src=”http://gist.github.com/448079.js?file=gistfile3.builder”></script&gt;


ActiveRecord and ActiveRelation


This is probably my favorite addition to Rails. The new ActiveRecord syntax is very similar to Doctrine (which we use with symfony). Instead of passing options as a hash to your find() method, they’ve created a bunch of new methods you can chain together.
<script src=”http://gist.github.com/448079.js?file=gistfile4.builder”></script&gt;

The best part is that any of the query methods just return a relation instead of running the sql query. That way, you can chain more query methods on the relation later. The actual query doesn’t run until you try to access the object(s) (such as with an iterator). This makes fragment caching a lot easier, because you can keep your query call in your action. Even if the cache is hit, it will only create a relation and not actually query the database. You can also force the query to be run at any time by calling all(), first(), or last() on the relation.

Advertisements
"New Features in Rails 3"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s