From the course: Ruby on Rails 5 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

One-to-one associations

One-to-one associations - Ruby on Rails Tutorial

From the course: Ruby on Rails 5 Essential Training

Start my 1-month free trial

One-to-one associations

- [Instructor] In this movie we'll learn how to implement one-to-one associations in Rails. Let's 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 single table. For example, customer has_one :billing_address. The…

Contents