Rails Validations vs Postgres Check Constraints
November 15, 2015
Before using Postgres, I would have to rely heavily on Rails validations to ensure data integrity—MySQL simply doesn’t provide the tools
to do this. This always felt wrong. Outside of rogue processes connecting to the database, there’s also application bugs
and, well, the ActiveRecord API itself: update_attribute
skips validation! That means it’s hard to rely on Rails validations for data
integrity, but there isn’t a great alternative for creating a great user experience.
Postgres has the answer: check constraints. As we’ll see, check constraints are tool for data integrity and work much better than Rails validations. But Rails validations are still a great tool for user experience.