From the course: Ruby on Rails Models and Associations
Unlock the full course today
Join today to access over 24,000 courses taught by industry experts.
Join tables during queries - Ruby on Rails Tutorial
From the course: Ruby on Rails Models and Associations
Join tables during queries
- [Instructor] It is common to join tables when working with relational databases. Using our associations, we can join tables for our active record queries too. It happens automatically when we use the many-to-many associations but I want to show you two methods to assist you in joining other tables. Joining tables can become complex. So we'll stick to the simple and most frequently used cases. If we want to retrieve additional data from another table, along with our main results, we want to use includes. You provide it with an array of the associations to add to the query. In this example, the query will retrieve all of the tasks as well as the categories associated with them. If you know the categories will need to be retrieved too, it can be better to find both at once. Rails includes logic to choose the most efficient way to retrieve these records. It may decide to write SQL with the left outer join or it may…
Contents
-
-
-
-
-
-
(Locked)
Overview of associations3m 57s
-
(Locked)
Create a one-to-many association2m 19s
-
(Locked)
Use a one-to-many association4m 53s
-
(Locked)
Destroy dependent related records4m 34s
-
(Locked)
Has and belongs to many associations5m 52s
-
(Locked)
Rich join associations5m 56s
-
(Locked)
Traverse a rich join association4m 17s
-
(Locked)
Join tables during queries5m 57s
-
(Locked)
-