From the course: Ruby on Rails 6 Essential Training: Models and Associations

Unlock the full course today

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

Work with associations

Work with associations - Ruby on Rails Tutorial

From the course: Ruby on Rails 6 Essential Training: Models and Associations

Start my 1-month free trial

Work with associations

- [Narrator] In this chapter, we're going to learn about Active Record Associations. Much of the power of Ruby on Rails lies in creating good associations to keep our data well organized, so that it can be used efficiently. There are three main association types. And this is not just for Ruby on Rails, it's for all relational databases. You can have a one-to-one, a one-to-many, or a many-to-many association. Let's take a look at each one of these. First, one-to-one associations. Those are often abbreviated as 1:1, and that's when you have one object that's related to one and only one other object. So for example, we might have a classroom and a teacher. The classroom has one and only one teacher assigned to it, and the teacher belongs to the classroom. To make this work in the database, you would have a classrooms table and you'd have a teacher's table, and you would put a foreign key on the teacher's table. So that…

Contents