From 8e98b43db0d8defdb01ff5fef4f34846d1cdb50b Mon Sep 17 00:00:00 2001 From: CassioMarques Date: Sat, 31 Jan 2009 14:45:35 -0200 Subject: Getting rid of some of the pre-existent content in the AR basics guide, adding new stuff to it. --- railties/doc/guides/html/active_record_basics.html | 178 ++++++++------------- railties/doc/guides/html/form_helpers.html | 6 +- railties/doc/guides/html/security.html | 2 +- .../doc/guides/source/active_record_basics.txt | 101 ++++++------ 4 files changed, 127 insertions(+), 160 deletions(-) diff --git a/railties/doc/guides/html/active_record_basics.html b/railties/doc/guides/html/active_record_basics.html index 6dad70a46f..f99d57faac 100644 --- a/railties/doc/guides/html/active_record_basics.html +++ b/railties/doc/guides/html/active_record_basics.html @@ -43,9 +43,6 @@ Active Record inside the MVC model
  • - Creating ActiveRecord models -
  • -
  • Convention over Configuration in ActiveRecord
  • - Philosophical Approaches & Common Conventions -
  • -
  • - ActiveRecord Magic -
  • -
  • - How ActiveRecord Maps your Database. + Creating ActiveRecord models
  • - Growing Your Database Relationships Naturally + Overriding the naming conventions
  • - Attributes + Validations
  • - Validations & Callbacks + Callbacks
  • @@ -179,43 +170,23 @@ Perform database operations in an object-oriented fashion.

    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.

    -

    5. Creating ActiveRecord 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:

    -
    -
    -
    class Product < ActiveRecord::Base; end
    -

    This will create a Product model, mapped to a products 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 products table was created using a SQL sentence like:

    -
    -
    -
    CREATE TABLE products (
    -   id int(11) NOT NULL auto_increment,
    -   name varchar(255),
    -   PRIMARY KEY  (id)
    -);
    -

    Following the table schema above, you would be able to write code like the following:

    -
    -
    -
    p = Product.new
    -p.name = "Some Book"
    -puts p.name # "Some Book"
    -
    -

    6. Convention over Configuration in ActiveRecord

    +

    5. Convention over Configuration in ActiveRecord

    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.

    -

    6.1. Naming Conventions

    +

    5.1. 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:

    -

    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 i.e. (book_clubs) +

      +
    • +
    • +

      +Model Class - Singular with the first letter of each word capitalized i.e. (BookClub) +

      +
    • +
    -

    6.2. Schema Conventions

    +

    5.2. Schema Conventions

    ActiveRecord uses naming conventions for the columns in database tables, depending on the purpose of these columns.

    • @@ -303,73 +274,66 @@ cellspacing="0" cellpadding="4"> 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.
    -
    -
    -
    == STOPED HERE
    -
    -
    -

    7. Philosophical Approaches & Common Conventions

    -
    -

    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

    -
    -

    8. ActiveRecord Magic

    -
    -
      -
    • -

      -timestamps -

      -
    • -
    • -

      -updates -

      -
    • -
    -

    9. How ActiveRecord Maps your Database.

    +

    6. Creating ActiveRecord models

    -
      -
    • -

      -sensible defaults -

      -
    • -
    • -

      -overriding conventions -

      -
    • -
    +

    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:

    +
    +
    +
    class Product < ActiveRecord::Base; end
    +

    This will create a Product model, mapped to a products 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 products table was created using a SQL sentence like:

    +
    +
    +
    CREATE TABLE products (
    +   id int(11) NOT NULL auto_increment,
    +   name varchar(255),
    +   PRIMARY KEY  (id)
    +);
    +

    Following the table schema above, you would be able to write code like the following:

    +
    +
    +
    p = Product.new
    +p.name = "Some Book"
    +puts p.name # "Some Book"
    -

    10. Growing Your Database Relationships Naturally

    +

    7. 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:

    +
    +
    +
    class Product < ActiveRecord::Base
    +  set_table_name "PRODUCT"
    +end
    +

    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:

    +
    +
    +
    class Product < ActiveRecord::Base
    +  set_primary_key "product_id"
    +end
    -

    11. Attributes

    +

    8. Validations

    -
      -
    • -

      -attribute accessor method. How to override them? -

      -
    • -
    • -

      -attribute? -

      -
    • -
    • -

      -dirty records - - -== ActiveRecord handling the CRUD of your Rails application - Understanding the life-cycle of an ActiveRecord -

      -
    • -
    +

    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 Active Record Validations and Callbacks guide.

    -

    12. Validations & Callbacks

    +

    9. Callbacks

    -

    see the Validations & Callbacks guide for more info.

    +

    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 Active Record Validations and Callbacks guide.

    diff --git a/railties/doc/guides/html/form_helpers.html b/railties/doc/guides/html/form_helpers.html index 2693f29a9a..d5e571ae31 100644 --- a/railties/doc/guides/html/form_helpers.html +++ b/railties/doc/guides/html/form_helpers.html @@ -197,7 +197,7 @@ Find out where to look for complex forms

    1.1. A Generic search form

    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:

    -
      +
      1. a form element with "GET" method, @@ -420,7 +420,7 @@ end <% end %>

    There are a few things to note here:

    -
      +
      1. :article is the name of the model and @article is the actual object being edited. @@ -662,7 +662,7 @@ output:

        4. Using Date and Time Form Helpers

        The date and time helpers differ from all the other form helpers in two important respects:

        -
          +
          1. 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 params hash with your date or time. diff --git a/railties/doc/guides/html/security.html b/railties/doc/guides/html/security.html index 371decda64..4751e9f92b 100644 --- a/railties/doc/guides/html/security.html +++ b/railties/doc/guides/html/security.html @@ -326,7 +326,7 @@ The user has his credit back.

        This attack focuses on fixing a user’s session id known to the attacker, and forcing the user’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:

        -
          +
          1. 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). diff --git a/railties/doc/guides/source/active_record_basics.txt b/railties/doc/guides/source/active_record_basics.txt index dd62b9a948..2336e162dc 100644 --- a/railties/doc/guides/source/active_record_basics.txt +++ b/railties/doc/guides/source/active_record_basics.txt @@ -44,35 +44,6 @@ It's easy to see that the Rails Active Record implementation goes way beyond the 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. -== Creating ActiveRecord 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: - -[source, ruby] ------------------------------------------------------------------- -class Product < ActiveRecord::Base; end ------------------------------------------------------------------- - -This will create a +Product+ model, mapped to a *products* 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 *products* table was created using a SQL sentence like: - -[source, sql] ------------------------------------------------------------------- -CREATE TABLE products ( - id int(11) NOT NULL auto_increment, - name varchar(255), - PRIMARY KEY (id) -); ------------------------------------------------------------------- - -Following the table schema above, you would be able to write code like the following: - -[source, ruby] ------------------------------------------------------------------- -p = Product.new -p.name = "Some Book" -puts p.name # "Some Book" ------------------------------------------------------------------- - == Convention over Configuration in ActiveRecord 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. @@ -81,8 +52,8 @@ When writing applications using other programming languages or frameworks, it ma 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: -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 i.e. (book_clubs) +* Model Class - Singular with the first letter of each word capitalized i.e. (BookClub) [width="60%", options="header"] |============================== @@ -111,28 +82,60 @@ There are also some optional column names that will create additional features t 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. - == STOPED HERE +== Creating ActiveRecord 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: -== Philosophical Approaches & Common Conventions -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 +[source, ruby] +------------------------------------------------------------------ +class Product < ActiveRecord::Base; end +------------------------------------------------------------------ + +This will create a +Product+ model, mapped to a *products* 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 *products* table was created using a SQL sentence like: + +[source, sql] +------------------------------------------------------------------ +CREATE TABLE products ( + id int(11) NOT NULL auto_increment, + name varchar(255), + PRIMARY KEY (id) +); +------------------------------------------------------------------ + +Following the table schema above, you would be able to write code like the following: + +[source, ruby] +------------------------------------------------------------------ +p = Product.new +p.name = "Some Book" +puts p.name # "Some Book" +------------------------------------------------------------------ + +== 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: +[source, ruby] +------------------------------------------------------------------ +class Product < ActiveRecord::Base + set_table_name "PRODUCT" +end +------------------------------------------------------------------ + +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: +[source, ruby] +------------------------------------------------------------------ +class Product < ActiveRecord::Base + set_primary_key "product_id" +end +------------------------------------------------------------------ -== ActiveRecord Magic - - timestamps - - updates +== Validations -== How ActiveRecord Maps your Database. -- sensible defaults -- overriding conventions +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 http://guides.rails.info/activerecord_validations_callbacks.html#_overview_of_activerecord_validation[Active Record Validations and Callbacks guide]. -== Growing Your Database Relationships Naturally +== Callbacks -== Attributes - - attribute accessor method. How to override them? - - attribute? - - dirty records - - -== ActiveRecord handling the CRUD of your Rails application - Understanding the life-cycle of an ActiveRecord +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 http://guides.rails.info/activerecord_validations_callbacks.html#_callbacks[Active Record Validations and Callbacks guide]. -== Validations & Callbacks -see the Validations & Callbacks guide for more info. -- cgit v1.2.3