Posts

Showing posts with the label one to one

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