diff options
author | CassioMarques <cassiommc@gmail.com> | 2009-01-28 00:21:27 -0200 |
---|---|---|
committer | CassioMarques <cassiommc@gmail.com> | 2009-01-28 00:21:50 -0200 |
commit | f7a6bc87abbaa49fb696f68e183849f94702b1b2 (patch) | |
tree | 29033b11d2bd769c167ab12efc711bfaa55edcab /railties/doc/guides/html | |
parent | 20e597538013958c844ce3d1356fb36fe5ead86e (diff) | |
download | rails-f7a6bc87abbaa49fb696f68e183849f94702b1b2.tar.gz rails-f7a6bc87abbaa49fb696f68e183849f94702b1b2.tar.bz2 rails-f7a6bc87abbaa49fb696f68e183849f94702b1b2.zip |
Advancing in the Active Record Basics guide
Diffstat (limited to 'railties/doc/guides/html')
-rw-r--r-- | railties/doc/guides/html/active_record_basics.html | 97 |
1 files changed, 68 insertions, 29 deletions
diff --git a/railties/doc/guides/html/active_record_basics.html b/railties/doc/guides/html/active_record_basics.html index 19275708a5..f46219ed39 100644 --- a/railties/doc/guides/html/active_record_basics.html +++ b/railties/doc/guides/html/active_record_basics.html @@ -43,10 +43,21 @@ <a href="#_active_record_inside_the_mvc_model">Active Record inside the MVC model</a> </li> <li> - <a href="#_active_record_the_engine_of_rails">Active Record The Engine of Rails</a> + <a href="#_creating_activerecord_models">Creating ActiveRecord models</a> + </li> + <li> + <a href="#_convention_over_configuration_in_activerecord">Convention over Configuration in ActiveRecord</a> <ul> - <li><a href="#_rails_active_record_conventions">Rails Active Record Conventions</a></li> + <li><a href="#_naming_conventions">Naming Conventions</a></li> + + </ul> + </li> + <li> + <a href="#_stoped_here">STOPED HERE</a> + <ul> + + <li><a href="#_schema_conventions">Schema Conventions</a></li> </ul> </li> @@ -131,6 +142,8 @@ Each instance of this class is mapped to a single row in the database table. </p>
</li>
</ul></div>
+<div class="paragraph"><p>The definition of the Active Record pattern in Martin Fowler’s words:</p></div>
+<div class="paragraph"><p>"<em>An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data."</em></p></div>
</div>
<h2 id="_object_relational_mapping">2. Object Relational Mapping</h2>
<div class="sectionbody">
@@ -152,6 +165,11 @@ Represent associations between these models. </li>
<li>
<p>
+Represent inheritance hierarquies through related models.
+</p>
+</li>
+<li>
+<p>
Validate models before they get recorded to the database.
</p>
</li>
@@ -165,27 +183,48 @@ Perform database operations in an object-oriented fashion. </div>
<h2 id="_active_record_inside_the_mvc_model">4. Active Record inside the MVC model</h2>
<div class="sectionbody">
+<div class="paragraph"><p>Active Record plays the role of model inside the MVC structure followed by Rails applications. Since model objects should encapsulate both state and logic of your applications, it’s ActiveRecord responsability to deliver you the easiest possible way to recover this data from the database.</p></div>
</div>
-<h2 id="_active_record_the_engine_of_rails">5. Active Record The Engine of Rails</h2>
+<h2 id="_creating_activerecord_models">5. Creating ActiveRecord models</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>It’s very easy to create ActiveRecord models. All you have to do is to subclass the ActiveRecord::Base class and you’re good to go:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Product <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will create a <tt>Product</tt> model, mapped to a <strong>products</strong> table at the database. By doing this you’ll also have the hability to map the columns of each row in that table with the attributes of the instances of your model. So, suppose that the <strong>products</strong> table was created using a SQL sentence like:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">CREATE</span></span> <span style="font-weight: bold"><span style="color: #0000FF">TABLE</span></span> products <span style="color: #990000">(</span>
+ id <span style="color: #009900">int</span><span style="color: #990000">(</span><span style="color: #993399">11</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">NOT</span></span> <span style="font-weight: bold"><span style="color: #0000FF">NULL</span></span> <span style="font-weight: bold"><span style="color: #0000FF">auto_increment</span></span><span style="color: #990000">,</span>
+ name <span style="color: #009900">varchar</span><span style="color: #990000">(</span><span style="color: #993399">255</span><span style="color: #990000">),</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">PRIMARY</span></span> <span style="font-weight: bold"><span style="color: #0000FF">KEY</span></span> <span style="color: #990000">(</span>id<span style="color: #990000">)</span>
+<span style="color: #990000">);</span></tt></pre></div></div>
+<div class="paragraph"><p>Following the table schema above, you would be able to write code like the following:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>p <span style="color: #990000">=</span> Product<span style="color: #990000">.</span>new
+p<span style="color: #990000">.</span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"Some Book"</span>
+puts p<span style="color: #990000">.</span>name <span style="font-style: italic"><span style="color: #9A1900"># "Some Book"</span></span></tt></pre></div></div>
+</div>
+<h2 id="_convention_over_configuration_in_activerecord">6. Convention over Configuration in ActiveRecord</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>When writing applications using other programming languages or frameworks, it may be necessary to write a lot of configuration code. This is particulary true for ORM frameworks in general. However, if you follow the conventions adopted by Rails, you’ll need to write very little configuration (in some case no configuration at all) when creating ActiveRecord models. The idea is that if you configure your applications in the very same way most of the times then this should be the default way. In this cases, explicity configuration would be needed only in those cases where you can’t follow the conventions for any reason.</p></div>
+<h3 id="_naming_conventions">6.1. Naming Conventions</h3>
+<div class="paragraph"><p>By default, ActiveRecord uses some naming conventions to find out how the mapping between models and database tables should be created. It uses two basic strategies to convert between class names and table names:</p></div>
+<h4 id="_pluralization">6.1.1. Pluralization</h4>
+<div class="paragraph"><p>Rails will pluralize your class names to find the respective database table. So, for a class <tt>Book</tt>, you should have a database table called <strong>books</strong>. The Rails pluralization mechanisms are very powerful, being capable to pluralize (and singularize) both regular and irregular words.</p></div>
+</div>
+<h2 id="_stoped_here">7. STOPED HERE</h2>
<div class="sectionbody">
-<div class="paragraph"><p>Active Record is a design pattern used to access data within a database. The name “Active Record” was coined by Martin Fowler in his book “Patterns of Enterprise Application Architecture”. Essentially, when a record is returned from the database instead of being just the data it is wrapped in a class, which gives you methods to control that data with. The rails framework is built around the MVC (Model View Controller) design patten and the Active Record is used as the default Model.</p></div>
-<div class="paragraph"><p>The Rails community added several useful concepts to their version of Active Record, including inheritance and associations, which are extremely useful for web applications. The associations are created by using a DSL (domain specific language) of macros, and inheritance is achieved through the use of STI (Single Table Inheritance) at the database level.</p></div>
-<div class="paragraph"><p>By following a few simple conventions the Rails Active Record will automatically map between:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Classes & Database Tables
-</p>
-</li>
-<li>
-<p>
-Class attributes & Database Table Columns
-</p>
-</li>
-</ul></div>
-<h3 id="_rails_active_record_conventions">5.1. Rails Active Record Conventions</h3>
-<div class="paragraph"><p>Here are the key conventions to consider when using Active Record.</p></div>
-<h4 id="_naming_conventions">5.1.1. Naming Conventions</h4>
<div class="paragraph"><p>Database Table - Plural with underscores separating words i.e. (book_clubs)
Model Class - Singular with the first letter of each word capitalized i.e. (BookClub)
Here are some additional Examples:</p></div>
@@ -249,7 +288,7 @@ cellspacing="0" cellpadding="4"> </tbody>
</table>
</div>
-<h4 id="_schema_conventions">5.1.2. Schema Conventions</h4>
+<h3 id="_schema_conventions">7.1. Schema Conventions</h3>
<div class="paragraph"><p>To take advantage of some of the magic of Rails database tables must be modeled
to reflect the ORM decisions that Rails makes.</p></div>
<div class="tableblock">
@@ -287,7 +326,7 @@ cellspacing="0" cellpadding="4"> </tbody>
</table>
</div>
-<h4 id="_magic_field_names">5.1.3. Magic Field Names</h4>
+<h4 id="_magic_field_names">7.1.1. Magic Field Names</h4>
<div class="paragraph"><p>When these optional fields are used in your database table definition they give the Active Record
instance additional features.</p></div>
<div class="admonitionblock">
@@ -424,11 +463,11 @@ http://www.gnu.org/software/src-highlite --> <div class="paragraph"><p>Rails further extends this model by giving each ActiveRecord a way of describing the variety of ways records are associated with one another. We will touch on some of these associations later in the guide but I encourage readers who are interested to read the guide to ActiveRecord associations for an in-depth explanation of the variety of ways rails can model associations.
- Associations between objects controlled by meta-programming macros.</p></div>
</div>
-<h2 id="_philosophical_approaches_amp_common_conventions">6. Philosophical Approaches & Common Conventions</h2>
+<h2 id="_philosophical_approaches_amp_common_conventions">8. Philosophical Approaches & Common Conventions</h2>
<div class="sectionbody">
<div class="paragraph"><p>Rails has a reputation of being a zero-config framework which means that it aims to get you off the ground with as little pre-flight checking as possible. This speed benefit is achieved by following “Convention over Configuration”, which is to say that if you agree to live with the defaults then you benefit from a the inherent speed-boost. As Courtneay Gasking put it to me once “You don’t want to off-road on Rails”. ActiveRecord is no different, while it’s possible to override or subvert any of the conventions of AR, unless you have a good reason for doing so you will probably be happy with the defaults. The following is a list of the common conventions of ActiveRecord</p></div>
</div>
-<h2 id="_activerecord_magic">7. ActiveRecord Magic</h2>
+<h2 id="_activerecord_magic">9. ActiveRecord Magic</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
@@ -443,7 +482,7 @@ updates </li>
</ul></div>
</div>
-<h2 id="_how_activerecord_maps_your_database">8. How ActiveRecord Maps your Database.</h2>
+<h2 id="_how_activerecord_maps_your_database">10. How ActiveRecord Maps your Database.</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
@@ -458,10 +497,10 @@ overriding conventions </li>
</ul></div>
</div>
-<h2 id="_growing_your_database_relationships_naturally">9. Growing Your Database Relationships Naturally</h2>
+<h2 id="_growing_your_database_relationships_naturally">11. Growing Your Database Relationships Naturally</h2>
<div class="sectionbody">
</div>
-<h2 id="_attributes">10. Attributes</h2>
+<h2 id="_attributes">12. Attributes</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
@@ -483,7 +522,7 @@ dirty records </li>
</ul></div>
</div>
-<h2 id="_validations_amp_callbacks">11. Validations & Callbacks</h2>
+<h2 id="_validations_amp_callbacks">13. Validations & Callbacks</h2>
<div class="sectionbody">
<div class="paragraph"><p>see the Validations & Callbacks guide for more info.</p></div>
</div>
|