All applications — particularly web applications that accept user data — need a solid foundation of data validation, and directly within the model is a great place to perform these initial validations. Proper validation will ensure that only valid data is processed and ultimately saved to your database.
Rails offers plenty of built-in validation helpers, or you can even create your own custom validation, so there’s no excuse not to validate your data as accurately as possible before saving it to the database.
As a simple example, here we’re validating that our book title exists and is no larger than 255 characters. We’re also validating that the ISBN is a unique integer between 10 and 13 characters.
Leave a Reply