{"id":3925,"date":"2015-05-04T21:38:07","date_gmt":"2015-05-05T01:38:07","guid":{"rendered":"http:\/\/webninjataylor.com\/library\/?p=3925"},"modified":"2015-05-04T22:01:52","modified_gmt":"2015-05-05T02:01:52","slug":"ruby-on-rails-validations","status":"publish","type":"post","link":"https:\/\/webninjataylor.com\/library\/ruby-on-rails-validations\/","title":{"rendered":"Ruby on Rails: Validations"},"content":{"rendered":"<ul>\n<li><strong>Validations are used to ensure that only valid data is saved into your database<\/strong>\n<ul>\n<li>Model-level validations are the best way\n<ul>\n<li>They are database agnostic, cannot be bypassed by end users, and are convenient to test and maintain<\/li>\n<\/ul>\n<\/li>\n<li>If any validations fail, the object will be marked as invalid and Active Record will not perform the INSERT or UPDATE operation<\/li>\n<\/ul>\n<\/li>\n<li>Validations are <strong>triggered by<\/strong> the methods create, create!, save, save!, update, and update!\n<ul>\n<li>The bang versions (e.g. save!) raise an exception if the record is invalid<\/li>\n<li>The non-bang versions don&#8217;t, save and update return false, create just returns the object<\/li>\n<\/ul>\n<\/li>\n<li>To verify whether or not an object is valid, Rails uses the <span class=\"code\">valid?<\/span> method<\/li>\n<li>The <span class=\"code\">errors<\/span> method can be run after validations<\/li>\n<li>Helper methods\n<ul>\n<li><span class=\"code\">acceptance<\/span> &#8230; validates a checkbox and can be a virtual attribute not stored in the database<\/li>\n<li><span class=\"code\">validates_associated<\/span> &#8230; when your model has associations with other models which also need validation\n<ul>\n<li>Don&#8217;t call on both ends or you&#8217;ll get an infinite loop<\/li>\n<\/ul>\n<\/li>\n<li><span class=\"code\">confirmation<\/span> &#8230; when two fields must match<\/li>\n<li><span class=\"code\">exclusion<\/span> &#8230; validates that the attributes&#8217; values are not included in a given set<\/li>\n<li><span class=\"code\">format<\/span> &#8230; to match a regex<\/li>\n<li><span class=\"code\">inclusion<\/span> &#8230; validates that the attributes&#8217; values are included in a given set<\/li>\n<li><span class=\"code\">length<\/span> &#8230; {minimum: 10}, {maximum: 500}, {in: 6..20}, {is: 2}<\/li>\n<li><span class=\"code\">numericality<\/span> &#8230;\u00a0validates that your attributes have only numeric values\n<pre>\/\/ e.g.\r\nnumericality: true\r\nnumericality: { only_integer: true }<\/pre>\n<\/li>\n<li><span class=\"code\">presence<\/span> &#8230; to make sure it&#8217;s not empty<\/li>\n<li><span class=\"code\">absence<\/span> &#8230; to make sure it&#8217;s empty<\/li>\n<li><span class=\"code\">uniqueness<\/span> &#8230;This helper validates that the attribute&#8217;s value is unique right before the object gets saved. It does not create a uniqueness constraint in the database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique. To avoid that, you must create a unique index on both columns in your database<\/li>\n<li><span class=\"code\">validates_with<\/span> &#8230; to pass the record to a separate class for\u00a0validation<\/li>\n<li><span class=\"code\">validates_each<\/span> &#8230; to validate attributes against a block<\/li>\n<li><span class=\"code\">allow_nil<\/span><\/li>\n<li><span class=\"code\">allow_blank<\/span><\/li>\n<li><span class=\"code\">message<\/span> &#8230; for error message<\/li>\n<li><span class=\"code\">on<\/span> &#8230; for when the validation should happen<\/li>\n<li>Conditional validation with <span class=\"code\">if<\/span> or <span class=\"code\">unless<\/span> (e.g.\u00a0<span class=\"code\">validates :card_number, presence: true, if: :paid_with_card?<\/span>)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Resources<\/h2>\n<ul>\n<li><a href=\"http:\/\/guides.rubyonrails.org\/active_record_validations.html\" target=\"_blank\">Active Record Validations<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Validations are used to ensure that only valid data is saved into your database Model-level validations are the best way They are database agnostic, cannot be bypassed by end users, and are convenient to test and maintain If any validations fail, the object will be marked as invalid and Active Record will not perform the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[144],"tags":[152],"class_list":["post-3925","post","type-post","status-publish","format-standard","hentry","category-web-shots","tag-ruby"],"_links":{"self":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/comments?post=3925"}],"version-history":[{"count":5,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3925\/revisions"}],"predecessor-version":[{"id":3930,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3925\/revisions\/3930"}],"wp:attachment":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/media?parent=3925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/categories?post=3925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/tags?post=3925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}