Posts

Showing posts with the label validation

Ruby Validation Methods

Image
Greetings Mate, In this article,   you will learn how to validate data   in Ruby on Rails.   Validations allow us to make sure   that data meets a set of requirements   before it's saved to the database.   You can think of them as rules   that our data must follow.   We could validate data every time   a form is submitted, in other words,   put our validation code in our controllers,   but that would make it so that the rules   are only enforced when we go through   one of those controller actions.   And then we'd be repeating ourselves   every time we had to go through a controller   that made updates to the same database tables. It's much better to enforce the rules   all the time globally in our application.   And since this concerns the application's   interaction with the database,   the model is the best place for it.   That way the rules will be enforced   right at t...

Belongs_To Presence Validation

Greetings Folks, In this blog, we're gonna see about the   belongs to presence validation, to make sure   that it doesn't become a problem for you.   Belongs to presence validation is a change in Rails 5.0,   It didn't exist before that.   Prior to Rails 5.0 an object that had a   belongs to relationship could be saved to the   database even if it didn't have a parent object   associated with it.   That is the belongs to relationship was not required   To be present.   Now, starting in Rails 5,   the belongs to relationship is not optional by default,   what happens behind the scenes is that when we  declare a belongs to, active record adds a validation   on the object for the related objects   presence... The parent object.   So if we have our page that means, that the page   won't save to the database.   Unless we have a subject associated with it.   Now we're gonna talk more abou...