Permalink
Browse files

Getting rid of some of the pre-existent content in the AR basics guid…

…e, adding new stuff to it.
  • Loading branch information...
1 parent cbf4bef commit 8e98b43db0d8defdb01ff5fef4f34846d1cdb50b @cassiomarques cassiomarques committed Jan 31, 2009
@@ -43,9 +43,6 @@ <h2 id="site_title_tagline">Sustainable productivity for web-application develop
<a href="#_active_record_inside_the_mvc_model">Active Record inside the MVC model</a>
</li>
<li>
- <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>
@@ -56,22 +53,16 @@ <h2 id="site_title_tagline">Sustainable productivity for web-application develop
</ul>
</li>
<li>
- <a href="#_philosophical_approaches_amp_common_conventions">Philosophical Approaches &amp; Common Conventions</a>
- </li>
- <li>
- <a href="#_activerecord_magic">ActiveRecord Magic</a>
- </li>
- <li>
- <a href="#_how_activerecord_maps_your_database">How ActiveRecord Maps your Database.</a>
+ <a href="#_creating_activerecord_models">Creating ActiveRecord models</a>
</li>
<li>
- <a href="#_growing_your_database_relationships_naturally">Growing Your Database Relationships Naturally</a>
+ <a href="#_overriding_the_naming_conventions">Overriding the naming conventions</a>
</li>
<li>
- <a href="#_attributes">Attributes</a>
+ <a href="#_validations">Validations</a>
</li>
<li>
- <a href="#_validations_amp_callbacks">Validations &amp; Callbacks</a>
+ <a href="#_callbacks">Callbacks</a>
</li>
</ol>
</div>
@@ -179,43 +170,23 @@ <h2 id="_active_record_inside_the_mvc_model">4. Active Record inside the MVC mod
<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&#8217;s ActiveRecord responsability to deliver you the easiest possible way to recover this data from the database.</p></div>
</div>
-<h2 id="_creating_activerecord_models">5. Creating ActiveRecord models</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>It&#8217;s very easy to create ActiveRecord models. All you have to do is to subclass the ActiveRecord::Base class and you&#8217;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">&lt;</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&#8217;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>
+<h2 id="_convention_over_configuration_in_activerecord">5. 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&#8217;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&#8217;t follow the conventions for any reason.</p></div>
-<h3 id="_naming_conventions">6.1. Naming Conventions</h3>
+<h3 id="_naming_conventions">5.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. 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. When using class names composed of two or more words, the model class name should follow the Ruby conventions, using the camelCase form, while the table name must contain the words separated by underscores. Examples:</p></div>
-<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)</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+Database Table - Plural with underscores separating words i.e. (book_clubs)
+</p>
+</li>
+<li>
+<p>
+Model Class - Singular with the first letter of each word capitalized i.e. (BookClub)
+</p>
+</li>
+</ul></div>
<div class="tableblock">
<table rules="all"
width="60%"
@@ -253,7 +224,7 @@ <h3 id="_naming_conventions">6.1. Naming Conventions</h3>
</tbody>
</table>
</div>
-<h3 id="_schema_conventions">6.2. Schema Conventions</h3>
+<h3 id="_schema_conventions">5.2. Schema Conventions</h3>
<div class="paragraph"><p>ActiveRecord uses naming conventions for the columns in database tables, depending on the purpose of these columns.</p></div>
<div class="ulist"><ul>
<li>
@@ -303,73 +274,66 @@ <h3 id="_schema_conventions">6.2. Schema Conventions</h3>
<td class="content">While these column names are optional they are in fact reserved by ActiveRecord. Steer clear of reserved keywords unless you want the extra functionality. For example, "type" is a reserved keyword used to designate a table using Single Table Inheritance. If you are not using STI, try an analogous keyword like "context", that may still accurately describe the data you are modeling.</td>
</tr></table>
</div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>== STOPED HERE</tt></pre>
-</div></div>
-</div>
-<h2 id="_philosophical_approaches_amp_common_conventions">7. Philosophical Approaches &amp; 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">8. ActiveRecord Magic</h2>
-<div class="sectionbody">
-<div class="ulist"><ul>
-<li>
-<p>
-timestamps
-</p>
-</li>
-<li>
-<p>
-updates
-</p>
-</li>
-</ul></div>
</div>
-<h2 id="_how_activerecord_maps_your_database">9. How ActiveRecord Maps your Database.</h2>
+<h2 id="_creating_activerecord_models">6. Creating ActiveRecord models</h2>
<div class="sectionbody">
-<div class="ulist"><ul>
-<li>
-<p>
-sensible defaults
-</p>
-</li>
-<li>
-<p>
-overriding conventions
-</p>
-</li>
-</ul></div>
+<div class="paragraph"><p>It&#8217;s very easy to create ActiveRecord models. All you have to do is to subclass the ActiveRecord::Base class and you&#8217;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">&lt;</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&#8217;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="_growing_your_database_relationships_naturally">10. Growing Your Database Relationships Naturally</h2>
+<h2 id="_overriding_the_naming_conventions">7. Overriding the naming conventions</h2>
<div class="sectionbody">
+<div class="paragraph"><p>What if you need to follow a different naming convention or need to use your Rails application with a legacy database? No problem, you can easily override the default conventions.</p></div>
+<div class="paragraph"><p>You can use the <tt>ActiveRecord::Base.set_table_name</tt> method to specify the table name that should be used:</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">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+ set_table_name <span style="color: #FF0000">"PRODUCT"</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>It&#8217;s also possible to override the column that should be used as the table&#8217;s primary key. Use the <tt>ActiveRecord::Base.set_primary_key</tt> method for that:</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">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+ set_primary_key <span style="color: #FF0000">"product_id"</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
-<h2 id="_attributes">11. Attributes</h2>
+<h2 id="_validations">8. Validations</h2>
<div class="sectionbody">
-<div class="ulist"><ul>
-<li>
-<p>
-attribute accessor method. How to override them?
-</p>
-</li>
-<li>
-<p>
-attribute?
-</p>
-</li>
-<li>
-<p>
-dirty records
- -
-== ActiveRecord handling the CRUD of your Rails application - Understanding the life-cycle of an ActiveRecord
-</p>
-</li>
-</ul></div>
+<div class="paragraph"><p>ActiveRecord gives the hability to validate the state of your models before they get recorded into the database. There are several methods that you can use to hook into the lifecycle of your models and validate that an attribute value is not empty or follow a specific format and so on. You can learn more about validations in the <a href="http://guides.rails.info/activerecord_validations_callbacks.html#_overview_of_activerecord_validation">Active Record Validations and Callbacks guide</a>.</p></div>
</div>
-<h2 id="_validations_amp_callbacks">12. Validations &amp; Callbacks</h2>
+<h2 id="_callbacks">9. Callbacks</h2>
<div class="sectionbody">
-<div class="paragraph"><p>see the Validations &amp; Callbacks guide for more info.</p></div>
+<div class="paragraph"><p>ActiveRecord callbacks allow you to attach code to certain events in the lifecycle of your models. This way you can add behaviour to your models by transparently executing code when those events occur, like when you create a new record, update it, destroy it and so on. You can learn more about callbacks in the <a href="http://guides.rails.info/activerecord_validations_callbacks.html#_callbacks">Active Record Validations and Callbacks guide</a>.</p></div>
</div>
</div>
@@ -197,7 +197,7 @@ <h2 id="_dealing_with_basic_forms">1. Dealing With Basic Forms</h2>
</div>
<h3 id="_a_generic_search_form">1.1. A Generic search form</h3>
<div class="paragraph"><p>Probably the most minimal form often seen on the web is a search form with a single text input for search terms. This form consists of:</p></div>
-<div class="olist arabic"><ol class="arabic">
+<div class="olist"><ol>
<li>
<p>
a form element with "GET" method,
@@ -420,7 +420,7 @@ <h3 id="_binding_a_form_to_an_object">2.2. Binding a form to an object</h3>
&lt;% end %&gt;</tt></pre>
</div></div>
<div class="paragraph"><p>There are a few things to note here:</p></div>
-<div class="olist arabic"><ol class="arabic">
+<div class="olist"><ol>
<li>
<p>
<tt>:article</tt> is the name of the model and <tt>@article</tt> is the actual object being edited.
@@ -662,7 +662,7 @@ <h3 id="_time_zone_and_country_select">3.4. Time zone and country select</h3>
<h2 id="_using_date_and_time_form_helpers">4. Using Date and Time Form Helpers</h2>
<div class="sectionbody">
<div class="paragraph"><p>The date and time helpers differ from all the other form helpers in two important respects:</p></div>
-<div class="olist arabic"><ol class="arabic">
+<div class="olist"><ol>
<li>
<p>
Dates and times are not representable by a single input element. Instead you have several, one for each component (year, month, day etc.) and so there is no single value in your <tt>params</tt> hash with your date or time.
@@ -326,7 +326,7 @@ <h3 id="_session_fixation">2.7. Session fixation</h3>
</div>
</div>
<div class="paragraph"><p>This attack focuses on fixing a user&#8217;s session id known to the attacker, and forcing the user&#8217;s browser into using this id. It is therefore not necessary for the attacker to steal the session id afterwards. Here is how this attack works:</p></div>
-<div class="olist arabic"><ol class="arabic">
+<div class="olist"><ol>
<li>
<p>
The attacker creates a valid session id: He loads the login page of the web application where he wants to fix the session, and takes the session id in the cookie from the response (see number 1 and 2 in the image).
Oops, something went wrong.

0 comments on commit 8e98b43

Please sign in to comment.