diff options
author | CassioMarques <cassiommc@gmail.com> | 2009-01-31 14:45:35 -0200 |
---|---|---|
committer | CassioMarques <cassiommc@gmail.com> | 2009-01-31 15:12:49 -0200 |
commit | 8e98b43db0d8defdb01ff5fef4f34846d1cdb50b (patch) | |
tree | 1b12848bdab051df197a5cf7f0eb4a6918a2f5f5 /railties/doc/guides/source | |
parent | cbf4bef492eb9e65e51019924e19346b1aba07ad (diff) | |
download | rails-8e98b43db0d8defdb01ff5fef4f34846d1cdb50b.tar.gz rails-8e98b43db0d8defdb01ff5fef4f34846d1cdb50b.tar.bz2 rails-8e98b43db0d8defdb01ff5fef4f34846d1cdb50b.zip |
Getting rid of some of the pre-existent content in the AR basics guide, adding new stuff to it.
Diffstat (limited to 'railties/doc/guides/source')
-rw-r--r-- | railties/doc/guides/source/active_record_basics.txt | 101 |
1 files changed, 52 insertions, 49 deletions
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. |