Posts

Showing posts with the label belongs to

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...

One to Many Association

Image
Greetings Geeks, Let's  understand how to work with  one-to-many associations .   One-to-many associations are gonna be similar   to one-to-one associations,   but there are a few main differences that I wanna highlight.   First, one-to-many associations   are gonna be much more commonly used.   In a typical application, you'll rarely use one-to-one,   but you'll use one-to-many the most often.   Second, because we have one object related to many objects,   we're gonna use plural names.   That's both gonna be true when we define the association   and also when we call the methods on it. And third, the relationship   is going to return an array of objects back to us   instead of a single object.   And that just makes sense, right?   If we're going to have many records associated with it,   we're going to have to store those in an array   so that it can hold all of those records...

One to one association

Image
Welcome,  First, consider   when you might actually use a one-to-one association   because there are two main use cases.   The most common reason is for unique items   that a person or a thing can have only one of.   For example, an employee has_one : office   or a student has_one : id_card .   The ID card is unique.   It belongs only to this one student   and to no one else.   And that student is only allowed to have one ID card.   It's certainly possible to imagine a scenario   where an employee might have two offices. But by using a has_one relationship,   we're rejecting that possibility in our design.   We've made it a statement of fact   that each employee can have only one.   At the point that we want to allow employees   to have more than one,   we would need to revise our relationships to match. The second reason to use a one-to-one relationship   is to break up a sing...