diff options
author | Trevor Turk <trevorturk@yahoo.com> | 2009-02-04 08:52:14 -0600 |
---|---|---|
committer | Trevor Turk <trevorturk@yahoo.com> | 2009-02-04 08:52:14 -0600 |
commit | edffab41120a2b4c4da20a3dd2d2314640d0d6cf (patch) | |
tree | a459a1354badbdef196e81a2effeb3acb66a8474 /railties/guides/source/activerecord_validations_callbacks.textile | |
parent | 8c9f0ef35aef098c5a87325de7f35686e43a4ee2 (diff) | |
parent | d39290cf0e582993bdfcd48f92544da174788c05 (diff) | |
download | rails-edffab41120a2b4c4da20a3dd2d2314640d0d6cf.tar.gz rails-edffab41120a2b4c4da20a3dd2d2314640d0d6cf.tar.bz2 rails-edffab41120a2b4c4da20a3dd2d2314640d0d6cf.zip |
Merge changes to Active Record Validations section
Diffstat (limited to 'railties/guides/source/activerecord_validations_callbacks.textile')
-rw-r--r-- | railties/guides/source/activerecord_validations_callbacks.textile | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/railties/guides/source/activerecord_validations_callbacks.textile b/railties/guides/source/activerecord_validations_callbacks.textile index 4dc41659e3..f7669e8977 100644 --- a/railties/guides/source/activerecord_validations_callbacks.textile +++ b/railties/guides/source/activerecord_validations_callbacks.textile @@ -55,33 +55,39 @@ CAUTION: There are many ways to change the state of an object in the database. S The following methods trigger validations, and will save the object to the database only if the object is valid. The bang versions (e.g. +save!+) will raise an exception if the record is invalid. The non-bang versions (e.g. +save+) simply return +false+. -* "+create+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002214 -* "+create!+":http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M002116 -* "+save+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002274 -* "+save!+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002275 -* "+update+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002215 -* "+update_attributes+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002281 -* "+update_attributes!+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002282 - -The following methods bypass validations, and will save the object to the database regardless of its validity. They should be used with caution: - -* "+decrement!+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002286 -* "+decrement_counter+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002224 -* "+increment!+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002284 -* "+increment_counter+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002223 -* "+toggle!+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002288 -* "+update_all+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002218 -* "+update_attribute+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002280 -* "+update_counters+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002222 - -Note that +save+ also has the ability to bypass validations if passed +false+. This technique should be used with caution: +* +create+ +* +create!+ +* +save+ +* +save!+ +* +update+ +* +update_attributes+ +* +update_attributes!+ + +The following methods skip validations, and will save the object to the database regardless of its validity. They should be used with caution: + +* +decrement!+ +* +decrement_counter+ +* +increment!+ +* +increment_counter+ +* +toggle!+ +* +update_all+ +* +update_attribute+ +* +update_counters+ + +Note that +save+ also has the ability to skip validations if passed +false+. This technique should be used with caution: * "+save(false)+":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002274 +h4. Skipping Validations + +TODO: Probably move the list above and mention save(false). Also mention that save(false) doesn't only skip the validations, but a lots of other callbacks too. + h4. Object#valid? To verify whether an object is valid, Active Record uses the +valid?+ method, which basically looks inside the object to see if it has any validation errors. These errors live in a collection that can be accessed through the +errors+ instance method. The process is really simple: If the +errors+ method returns an empty collection, the object is valid and can be saved. Each time a validation fails, an error message is added to the +errors+ collection. +h4. Object#invalid? + h3. Declarative Validation Helpers Active Record offers many pre-defined validation helpers that you can use directly inside your class definitions. These helpers create validation rules that are commonly used. Every time a validation fails, an error message is added to the object's +errors+ collection, and this message is associated with the field being validated. |