mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Active Record Validations and Callbacks guide: Fixed typos and rephrased some paragraphs for clarity
This commit is contained in:
parent
c09cd192aa
commit
2e4b741111
1 changed files with 12 additions and 11 deletions
|
@ -14,7 +14,7 @@ After reading this guide and trying out the presented concepts, we hope that you
|
||||||
|
|
||||||
endprologue.
|
endprologue.
|
||||||
|
|
||||||
h3. The Object Lifecycle
|
h3. The Object Life Cycle
|
||||||
|
|
||||||
During the normal operation of a Rails application, objects may be created, updated, and destroyed. Active Record provides hooks into this <em>object life cycle</em> so that you can control your application and its data.
|
During the normal operation of a Rails application, objects may be created, updated, and destroyed. Active Record provides hooks into this <em>object life cycle</em> so that you can control your application and its data.
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ We can see how it works by looking at some +rails console+ output:
|
||||||
=> false
|
=> false
|
||||||
</shell>
|
</shell>
|
||||||
|
|
||||||
Creating and saving a new record will send an SQL +INSERT+ operation to the database. Updating an existing record will send an SQL +UPDATE+ operation instead. Validations are typically run before these commands are sent to the database. If any validations fail, the object will be marked as invalid and Active Record will not perform the +INSERT+ or +UPDATE+ operation. This helps to avoid storing an invalid object in the database. You can choose to have specific validations run when an object is created, saved, or updated.
|
Creating and saving a new record will send a SQL +INSERT+ operation to the database. Updating an existing record will send a SQL +UPDATE+ operation instead. Validations are typically run before these commands are sent to the database. If any validations fail, the object will be marked as invalid and Active Record will not perform the +INSERT+ or +UPDATE+ operation. This helps to avoid storing an invalid object in the database. You can choose to have specific validations run when an object is created, saved, or updated.
|
||||||
|
|
||||||
CAUTION: There are many ways to change the state of an object in the database. Some methods will trigger validations, but some will not. This means that it's possible to save an object in the database in an invalid state if you aren't careful.
|
CAUTION: There are many ways to change the state of an object in the database. Some methods will trigger validations, but some will not. This means that it's possible to save an object in the database in an invalid state if you aren't careful.
|
||||||
|
|
||||||
|
@ -984,7 +984,7 @@ h3. Halting Execution
|
||||||
|
|
||||||
As you start registering new callbacks for your models, they will be queued for execution. This queue will include all your model's validations, the registered callbacks, and the database operation to be executed.
|
As you start registering new callbacks for your models, they will be queued for execution. This queue will include all your model's validations, the registered callbacks, and the database operation to be executed.
|
||||||
|
|
||||||
The whole callback chain is wrapped in a transaction. If any before callback method returns exactly +false+ or raises an exception the execution chain gets halted and a ROLLBACK is issued. After callbacks can only accomplish that by raising an exception.
|
The whole callback chain is wrapped in a transaction. If any <em>before</em> callback method returns exactly +false+ or raises an exception the execution chain gets halted and a ROLLBACK is issued; <em>after</em> callbacks can only accomplish that by raising an exception.
|
||||||
|
|
||||||
WARNING. Raising an arbitrary exception may break code that expects +save+ and friends not to fail like that. The +ActiveRecord::Rollback+ exception is thought precisely to tell Active Record a rollback is going on. That one is internally captured but not reraised.
|
WARNING. Raising an arbitrary exception may break code that expects +save+ and friends not to fail like that. The +ActiveRecord::Rollback+ exception is thought precisely to tell Active Record a rollback is going on. That one is internally captured but not reraised.
|
||||||
|
|
||||||
|
@ -1020,7 +1020,7 @@ Like in validations, we can also make our callbacks conditional, calling them on
|
||||||
|
|
||||||
h4. Using +:if+ and +:unless+ with a Symbol
|
h4. Using +:if+ and +:unless+ with a Symbol
|
||||||
|
|
||||||
You can associate the +:if+ and +:unless+ options with a symbol corresponding to the name of a method that will get called right before the callback. If this method returns +false+ the callback won't be executed. This is the most common option. Using this form of registration it's also possible to register several different methods that should be called to check if the callback should be executed.
|
You can associate the +:if+ and +:unless+ options with a symbol corresponding to the name of a method that will get called right before the callback. When using the +:if+ option, the callback won't be executed if the method returns +false+; when using the +:unless+ option, the callback won't be executed if the method returns +true+. This is the most common option. Using this form of registration it's also possible to register several different methods that should be called to check if the callback should be executed.
|
||||||
|
|
||||||
<ruby>
|
<ruby>
|
||||||
class Order < ActiveRecord::Base
|
class Order < ActiveRecord::Base
|
||||||
|
@ -1135,7 +1135,7 @@ Observers are conventionally placed inside of your +app/models+ directory and re
|
||||||
config.active_record.observers = :user_observer
|
config.active_record.observers = :user_observer
|
||||||
</ruby>
|
</ruby>
|
||||||
|
|
||||||
As usual, settings in +config/environments+ take precedence over those in +config/environment.rb+. So, if you prefer that an observer not run in all environments, you can simply register it in a specific environment instead.
|
As usual, settings in +config/environments+ take precedence over those in +config/environment.rb+. So, if you prefer that an observer doesn't run in all environments, you can simply register it in a specific environment instead.
|
||||||
|
|
||||||
h4. Sharing Observers
|
h4. Sharing Observers
|
||||||
|
|
||||||
|
@ -1162,6 +1162,7 @@ h3. Changelog
|
||||||
|
|
||||||
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213/tickets/26-active-record-validations-and-callbacks
|
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213/tickets/26-active-record-validations-and-callbacks
|
||||||
|
|
||||||
|
* July 20, 2010: Fixed typos and rephrased some paragraphs for clarity. "Jaime Iniesta":http://jaimeiniesta.com
|
||||||
* May 24, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
|
* May 24, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
|
||||||
* May 15, 2010: Validation Errors section updated by "Emili Parreño":http://www.eparreno.com
|
* May 15, 2010: Validation Errors section updated by "Emili Parreño":http://www.eparreno.com
|
||||||
* March 7, 2009: Callbacks revision by Trevor Turk
|
* March 7, 2009: Callbacks revision by Trevor Turk
|
||||||
|
|
Loading…
Reference in a new issue