Rails 2: The Top Five Features List
In Rails 2, you can significantly cut down on the amount of typing involved for these sorts of declarations by using a condensed syntax that allows you to combine columns of like-types together. Revisiting the previous migration, you can rewrite it like so:
class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :name, :email t.text :bio t.datetime :created_at, :updated_at end end def self.down drop_table :chapter_comments end end
Feature #4: Active Record Data and XML/JSON
For instance, you can use the new version 2 features to retrieve your del.icio.us bookmarks in JSON format (as an example, see my del.icio.us Rails links in JSON format here) and store them in a local database table using the from_json method. You then can retrieve the data from your database and expose it in JSON format using the to_json method, like so:
@links = Link.find(:all) render :json => @links.to_json()
If you wanted just to expose the link URLs in JSON format, you can use the :only attribute:
@links = Link.find(:all) render :json => @links.to_json(:only => url)
Feature #5: Performance and Maintainability Upgrades
This past year has seen a tremendous amount of debate regarding Rails' ability to scale. Regardless of which side of the fence you sit regarding this discussion, it probably doesn't come as a surprise that the Rails community took special interest in addressing various performance-related matters in the 2.0 release. Likewise, in response to the increased use of Rails within particularly complex, high-traffic environments, several maintainability enhancements made their way into the new version. I'll close out my homage to "the list" with a quick summary of my favorite new features in regards to both performance and maintainability:
Improved Configuration Management
The config/environment.rb file contains many of your Rails project's key configuration directives, such as the current working environment, which version of Rails your application is using, and the organization of various load paths. However, because the information in this file is globally available to your application, it tends to become the catch-all location for required configuration data used by things such as external libraries. To help you more effectively organize configuration data, you'll find a new directory in config called initializers. You can use this directory to store configuration data in well-organized files, thereby leaving environment.rb to manage only those configuration directives that are originally found in the file.
Keep in mind this will work only when your application is running in production mode. Once the change is made, restart your server (in production mode), reload the page, and check the source again. This time, you'll see just a single request:
Although no future release of Rails will be as groundbreaking as the original, rest assured that Rails 2.0 is well worth the upgrade. I hope this "list" gave you some insight into why!
About the Author
W. Jason Gilmore is co-founder of IT Enlightenment. He's the author of several books, including the best-selling Beginning PHP and MySQL 5: Novice to Professional, Second Edition (Apress, 2006. 913pp.). Jason loves receiving email, so don't hesitate to write him at wjATwjgilmore.com.
Page 2 of 2