Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Validations 101


Published on

Intro to validations in rails, code at

Published in: Internet
  • Be the first to comment

  • Be the first to like this

Validations 101

  1. 1. Validations 101 Or: How I transitioned from Miley Cyrus to Michael Bay Jokes
  2. 2. The preamble • All the code is available on github https:// • @tjridge •
  3. 3. Validations are the bees knees. • Ensures only valid data is saved to the database • Typically run before SQL insert/update commands sent to the database. • create/save/update methods trigger validations, if called with a bang invalid records will return an exception, rather than the object. • Some methods like update_column and update_attribute skip validation. save(:validate => false) skips validations as well. Be as wary of these as you would be of good reviews for a Michael Bay film.
  4. 4. Valid? Invalid? • valid? Triggers validations, returns true if no errors are found. • invalid? Does the inverse. • Object is valid if no errors found • could be technically invalid, but not return any errors until save or valid? etc is called. • Access error messages using .errors.messages method on object.
  5. 5. Validation Helpers • acceptance • associated • confirmation • exclusion • format • inclusion • length • numericality • presence • absence • uniqueness
  6. 6. Validation options • if • unless • on (:create, :update) • lets look at some code