Browse files

Update Guides, Active Record Validations, better explanation of what …

…methods bypass validations
  • Loading branch information...
1 parent 3584d8c commit 1ca565a16a6f966ba9f434b439c385b492a716df Trevor Turk committed Feb 4, 2009
Showing with 28 additions and 2 deletions.
  1. +28 −2 railties/guides/source/activerecord_validations_callbacks.textile
@@ -49,9 +49,35 @@ We can see how it works by looking at some script/console output:
=> false
-Saving new records means sending an SQL +INSERT+ operation to the database, while saving existing records (by calling either +save+ or +update_attributes+) will result in a SQL +UPDATE+ operation. Active Record will use these facts to perform validations upon your objects, keeping them out of the database if their inner state is invalid in some way. You can specify validations that will be beformed every time a object is saved, just when you're creating a new record or when you're updating an existing one.
+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 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 trigger the +INSERT+ or +UPDATE+ operation. This helps to avoid storing an object in the database that's invalid. You can choose to have specific validations run when an object is created, saved, or updated.
-CAUTION: There are four methods that when called will trigger validation: +save+, +save!+, +update_attributes+ and +update_attributes!+. There is one update method for Active Record objects left, which is +update_attribute+. This method will update the value of an attribute _without_ triggering any validation. Be careful when using +update_attribute+, because it can let you save your objects in an invalid state.
+CAUTION: There are many ways to change the state of an object in the database. Some will trigger validations, and some will not. This means it is possible to save an object in the database that's in an invalid state. Be careful when using Active Record methods that bypass validations.
+The following methods trigger validations and generally save the object to the database:
+* "create": (creates an object, but does not save to the database)
+* "create!":
+* "save":
+* "save!":
+* "update":
+* "update_attributes":
+* "update_attributes!":
+The following methods _do not_ trigger validations, but _do_ change the state of the object in the database. They should be used with caution:
+* "decrement!":
+* "decrement_counter":
+* "increment!":
+* "increment_counter":
+* "toggle":
+* "toggle!":
+* "update_all":
+* "update_attribute":
+* "update_counters":
+Note that +save+ has the ability to bypass validations. This technique should be used with caution:
+* "save(false)":
h4. Object#valid?

0 comments on commit 1ca565a

Please sign in to comment.