aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source/active_record_basics.textile
diff options
context:
space:
mode:
authorrspeicher <rspeicher@gmail.com>2010-06-14 18:11:35 -0400
committerrspeicher <rspeicher@gmail.com>2010-06-14 18:11:35 -0400
commitef2798f018d98dce28566a1679b7b85cd6acf75e (patch)
treec2007d8d1f72126431a6f7f453ed4973b0acc832 /railties/guides/source/active_record_basics.textile
parent5cd3c2a8556c8b1391508508c0f2ddfe89214028 (diff)
downloadrails-ef2798f018d98dce28566a1679b7b85cd6acf75e.tar.gz
rails-ef2798f018d98dce28566a1679b7b85cd6acf75e.tar.bz2
rails-ef2798f018d98dce28566a1679b7b85cd6acf75e.zip
Active Record Basics - Style and grammar edits
- "i.e." should be "e.g." in these cases, and include the comma - Newline consistency before/after Ruby blocks - Conform to the apparent guide standard of "Active Record" over "ActiveRecord"
Diffstat (limited to 'railties/guides/source/active_record_basics.textile')
-rw-r--r--railties/guides/source/active_record_basics.textile38
1 files changed, 21 insertions, 17 deletions
diff --git a/railties/guides/source/active_record_basics.textile b/railties/guides/source/active_record_basics.textile
index d81e461e63..57477b44c5 100644
--- a/railties/guides/source/active_record_basics.textile
+++ b/railties/guides/source/active_record_basics.textile
@@ -32,16 +32,16 @@ Active Record gives us several mechanisms, the most important being the ability
* Validate models before they get persisted to the database
* Perform database operations in an object-oriented fashion.
-h3. Convention over Configuration in ActiveRecord
+h3. Convention over Configuration in Active Record
-When writing applications using other programming languages or frameworks, it may be necessary to write a lot of configuration code. This is particularly 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, explicit configuration would be needed only in those cases where you can't follow the conventions for any reason.
+When writing applications using other programming languages or frameworks, it may be necessary to write a lot of configuration code. This is particularly 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 Active Record 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, explicit configuration would be needed only in those cases where you can't follow the conventions for any reason.
h4. Naming Conventions
-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 +Book+, you should have a database table called *books*. 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:
+By default, Active Record 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 +Book+, you should have a database table called *books*. 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:
-* 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)
+* Database Table - Plural with underscores separating words (e.g., book_clubs)
+* Model Class - Singular with the first letter of each word capitalized (e.g., BookClub)
|_.Model / Class |_.Table / Schema |
|Post |posts|
@@ -53,12 +53,12 @@ By default, ActiveRecord uses some naming conventions to find out how the mappin
h4. Schema Conventions
-ActiveRecord uses naming conventions for the columns in database tables, depending on the purpose of these columns.
+Active Record uses naming conventions for the columns in database tables, depending on the purpose of these columns.
-* *Foreign keys* - These fields should be named following the pattern table_id i.e. (item_id, order_id). These are the fields that ActiveRecord will look for when you create associations between your models.
-* *Primary keys* - By default, ActiveRecord will use an integer column named "id" as the table's primary key. When using "Rails Migrations":migrations.html to create your tables, this column will be automatically created.
+* *Foreign keys* - These fields should be named following the pattern table_id (e.g., item_id, order_id). These are the fields that Active Record will look for when you create associations between your models.
+* *Primary keys* - By default, Active Record will use an integer column named "id" as the table's primary key. When using "Rails Migrations":migrations.html to create your tables, this column will be automatically created.
-There are also some optional column names that will create additional features to ActiveRecord instances:
+There are also some optional column names that will create additional features to Active Record instances:
* *created_at / created_on* - ActiveRecord will store the current date and time to this field when creating the record.
* *updated_at / updated_on* - ActiveRecord will store the current date and times to this field when updating the record.
@@ -66,17 +66,17 @@ There are also some optional column names that will create additional features t
* *type* - Specifies that the model uses "Single Table Inheritance":http://api.rubyonrails.com/classes/ActiveRecord/Base.html
* *(table_name)_count* - Used to cache the number of belonging objects on associations. For example, a +comments_count+ column in a +Post+ class that has many instances of +Comment+ will cache the number of existent comments for each post.
-NOTE: 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.
+NOTE: While these column names are optional they are in fact reserved by Active Record. 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.
-h3. Creating ActiveRecord Models
+h3. Creating Active Record Models
-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:
+It's very easy to create Active Record models. All you have to do is to subclass the +ActiveRecord::Base+ class and you're good to go:
<ruby>
class Product < ActiveRecord::Base; end
</ruby>
-This will create a +Product+ model, mapped to a *products* table at the database. By doing this you'll also have the ability to map the columns of each row in that table with the attributes of the instances of your model. So, suppose that the *products* table was created using a SQL sentence like:
+This will create a +Product+ model, mapped to a *products* table at the database. By doing this you'll also have the ability to map the columns of each row in that table with the attributes of the instances of your model. So, suppose that the *products* table was created using an SQL sentence like:
<sql>
CREATE TABLE products (
@@ -99,12 +99,15 @@ h3. Overriding the Naming Conventions
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.
You can use the +ActiveRecord::Base.set_table_name+ method to specify the table name that should be used:
+
<ruby>
class Product < ActiveRecord::Base
set_table_name "PRODUCT"
end
</ruby>
+
If you do so, you will have to define manually the class name that is hosting the fixtures (class_name.yml) using the +set_fixture_class+ method in your test definition:
+
<ruby>
class FunnyJoke < ActiveSupport::TestCase
set_fixture_class :funny_jokes => 'Joke'
@@ -113,7 +116,8 @@ class FunnyJoke < ActiveSupport::TestCase
end
</ruby>
-It's also possible to override the column that should be used as the table's primary key. Use the +ActiveRecord::Base.set_primary_key+ method for that:
+It's also possible to override the column that should be used as the table's primary key using the +ActiveRecord::Base.set_primary_key+ method:
+
<ruby>
class Product < ActiveRecord::Base
set_primary_key "product_id"
@@ -122,7 +126,7 @@ end
h3. Reading and Writing Data
-CRUD is an acronym for the four verbs we use to operate on data: Create, Read, Update, Delete. Active Record automatically creates methods to allow an application to read and manipulate data stored within its tables.
+CRUD is an acronym for the four verbs we use to operate on data: *C*reate, *R*ead, *U*pdate and *D*elete. Active Record automatically creates methods to allow an application to read and manipulate data stored within its tables.
h4. Create
@@ -155,7 +159,7 @@ Finally, passing a block to either create or new will return a new User object:
h4. Read
-ActiveRecord provides a rich API for accessing data within a database. Below are a few examples of different data access methods provided by ActiveRecord.
+Active Record provides a rich API for accessing data within a database. Below are a few examples of different data access methods provided by Active Record.
<ruby>
# return all records
@@ -163,7 +167,7 @@ ActiveRecord provides a rich API for accessing data within a database. Below are
</ruby>
<ruby>
- # return first record
+ # return the first record
user = User.first
</ruby>