From the course: Ruby on Rails Models and Associations

Unlock the full course today

Join today to access over 24,200 courses taught by industry experts.

Overview of associations

Overview of associations

- [Instructor] In this chapter, we'll work with active record associations beginning with an overview. Associations keep our application's data well organized so that it can be used efficiently. Relational databases have three main association types one to one, one to many, and many to many. One to one associations relate one record to exactly one other record. An example would be one classroom that has one and only one teacher assigned to it. We would say a classroom has one teacher and a teacher belongs to a classroom. To make this work in the database, you'd put a column on the teacher's table to hold a foreign key, probably something like classroom ID. In a one to one association, which of these tables holds the foreign key is a design choice. They could be reversed. With that relationship established, if you have a classroom record and you want to find out what teacher belongs to it, you take its ID and search…

Contents