Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Ride the Web Application Express with Ruby on Rails : Page 4

Looking for productivity improvements in your Web application development? Take a look at a Web application framework that takes fewer lines of code than words in this sentence to have your first application up and running.

The Magic Behind Rails
So how did this all happen with just one line of code change? In a word, it is metaprogramming. The model class you developed, or more precisely, generated from the script, inherits from Ruby on Rails' ActiveRecord class. ActiveRecord objects dynamically infer their attributes from the table definition that they are linked to and you linked the ContactType to the contact_type database table by using Rails naming conventions. Rails naming conventions work to bridge the plural/singular difference from a table containing many to a singular instance of the model. For example, note that the database table is contact_types while an instance of the model comes from ContactType class. Yep, Rails is smart enough to figure out that instances of ContactType should probably be stored in a contact_types database table.

Figure 9. Rails Immediately Reflects Change: Changes to the database are immediately reflecting in Rails models and views as a result of using the Rails framework. Here, a new column called new_column is added to contact_type and it is immediately reflected in the view with no restart or code changes.

The view and controller are no less remarkable in Rails. The ContactTypeController class generated by the Rails script descends from ActiveController. The one line of code you added to