From the course: Ruby on Rails 5 Essential Training
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Relationship types - Ruby on Rails Tutorial
From the course: Ruby on Rails 5 Essential Training
Relationship types
- [Instructor] In the previous chapter, when we were creating, reading, updating, and deleting records, we were working with a single model, in a single database table, but what if we want to work with the data in related tables? After all, that's kind of the point of working with relational databases. We could query for a record, then read the value of its foreign key, and then make a second query using that foreign key in our query conditions to find the related record. That would be a valid approach, but it's a little tedious. It would be better if we could define relationships between our models, and therefore between our tables, and then work with those relationships in an object oriented way, and let Rails handle the behind the scenes work for us. ActiveRecord provides something called associations that allow us to define these relationships. Let's start by talking about the different relationship types, and then learn how Rails handles each one. There are three main relational…
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.