title: Ruby on Rails: Is it better to validate in the model or the database?
body: <p>Is it generally better practice (and why) to validate attributes in the model or in the database definition?</p>
  
  <p>For (a trivial) example:</p>
  
  <p>In the user model:</p>
  
  <pre><code>validates_presence_of :name
  </code></pre>
  
  <p>versus in the migration:</p>
  
  <pre><code>t.string :name, :null =&gt; false 
  </code></pre>
  
  <p>On the one hand, including it in the database seems more of a guarantee against any type of bad data sneaking in.  On the other hand, including it in the model makes things more transparent and easier to understand by grouping it in the code with the rest of the validations.  I also considered doing both, but this seems both un-DRY and less maintainable.</p>