DEV Community

Brian Waddell
Brian Waddell

Posted on

Learning Loads

Today I was able to learn about validations and creating sample data on the back end of my application. Validations are important useful tool to help ensure that a user includes important information within a required field. For example if you need a certain input from the user to be unique like a username, you can use validations. If we want to validate the presence of something we use validates :body (the_name_of_form_weWantChecked) presence: true Some associations perform validation checks for us. For example having an association belongs_to and the foreign keys associated with this accessor association will preform a automatic check. In short, Foreign keys will automatically be validated. Validations will overall help with security by making sure certain fields must be attended to. Be careful when validating certain fields. I added a validation to my photo models that a fan could only like the comment once but when I added sample data. The sample data would randomly like a photo and sometimes the follower would like the photo twice which violates my validation and caused an error to occur I had to make sure that the follower would only like the photo once with unless photo.fans.include?(follower) which basically says that if the follower has already like the photo they can not do this again which respects my first validation. Learning about validations has been an exciting journey. But I know this is just the tip of the iceberg.

Top comments (1)

Collapse
 
polaroidkidd profile image
Daniel Einars

Hi!

Welcome to DevTo and thanks for sharing this content! I thought you'd be happy to find out that the editor here supports markdown. That means you can format code. For example JavaScript

const hello = "world";
console.log("hello ", hello);
Enter fullscreen mode Exit fullscreen mode