September 17, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Rails 2: The Top Five Features List

  • January 7, 2008
  • By Jason Gilmore
  • Send Email »
  • More Articles »

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

A recent large Rails-driven project I was involved in called for the publishing of Active Record data in JSON format (JavaScript Object Notation). JSON is a lightweight data format that makes it very easy to pass data among a variety of languages, in my case between Ruby and JavaScript (namely, the Ext JS JavaScript framework). Rails has long supported JSON to varying degrees, but only with Rails 2 can you really kick your JSON support up a notch.

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.

Improved JavaScript Loading Performance

If you've ever taken advantage of Rails' built-in JavaScript capabilities by including <%= javascript_include_tag :defaults %> and then viewed the page source, you'll see there were actually five JavaScript files that the browser requested:

<script src="/javascripts/prototype.js?1198788702"
        type="text/javascript"></script>
<script src="/javascripts/effects.js?1198788702"
        type="text/javascript"></script>
<script src="/javascripts/dragdrop.js?1198788703"
        type="text/javascript"></script>
<script src="/javascripts/controls.js?1198788703"
        type="text/javascript"></script>
<script src="/javascripts/application.js?1198788703"
        type="text/javascript"></script>

Although the javascript_include_tag makes it convenient for the programmer to add these default libraries, the result is five additional requests to retrieve these files. You can reduce these requests to one by adding the :cache attribute to the tag, like so:

<%= javascript_include_tag :defaults, :cache => true %>

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:

<script src="/javascripts/all.js?1198796365"
        type="text/javascript"></script>

Conclusion

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



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel