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 - Ruby on Rails Tutorial
From the course: Ruby on Rails 5 Essential Training
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
Relationship types7m 2s
-
(Locked)
One-to-one associations9m 42s
-
(Locked)
One-to-many associations7m 54s
-
(Locked)
belongs_to presence validation4m 18s
-
(Locked)
Many-to-many associations: Simple9m 58s
-
(Locked)
Many-to-many associations: Rich10m 17s
-
(Locked)
Traverse a rich association5m 24s
-
(Locked)
-
-
-
-
-
-
-
-
-
-