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-many associations

One-to-many associations - Ruby on Rails Tutorial

From the course: Ruby on Rails 5 Essential Training

Start my 1-month free trial

One-to-many associations

- [Host] In the last movie, we saw how to use one-to-one associations. Now let's build on that to 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. We'll see those in a moment. 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. You use a one-to-many association when…

Contents