Friday, March 09, 2007

Courtesy of ActiveRecord

Courtesy的意思是:周到殷勤的举动。ActiveRecord的确是太殷勤了,就像一个英国管家,把各种打猎器具都准备好了,主人想干什么了,管家就会选好器具递到眼前。

In the edition/show.rhtml template, notice that a number of method calls to the object @edition—and double-barreled method calls, like @edition.work.title—are used to extract the information necessary to complete the view.

Again, none of these methods had to be defined manually. Some of them exist as a result of directives in the model file—specifically, the directive belongs_to :work in the file edition.rb.
ActiveRecord model files containing association directives (belongs_to, has_many) will prompt Rails to engineer the relationships among entities that you need.

Some, such as year and price, exist because the editions table in the database has fields with those names.

The methods spring into being, courtesy of ActiveRecord, so that you can pass information back and forth between the database records and your program using simple Ruby method-call syntax.

ActiveRecord models are the Ruby incarnation(化身) of the same domain universe that governs your database design.

No comments: