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.
Joining tables during queries - Ruby on Rails Tutorial
From the course: Ruby on Rails 6 Essential Training: Models and Associations
Joining tables during queries
- [Instructor] Once we have associations between our models, we can start to work with them in interesting ways. And one of those ways is by joining tables during our queries. If you're familiar with working with databases, you'll know that there are times when we want to be able to join two different tables together during a query. So we can query columns on more than one table. In Rails we can do that using the joins method. You see, I have product.joins, and then whatever tables I want to join together. That's the name of the relationship that's established in the model, and then whatever clauses or order statements or anything else that I want to have after it just like normal. But you'll notice that inside that query string, I've modified it a little bit. Instead of being product active, I've got categories.active. That is I'm taking a product, I'm joining it to the categories table, and I'm asking SQL to look at the…
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.
Contents
-
-
-
-
-
-
(Locked)
Work with associations7m 35s
-
(Locked)
Validate presence with belongs_to2m 39s
-
(Locked)
Destroy dependent-related records3m 10s
-
(Locked)
Has and belongs to many relationships10m 3s
-
(Locked)
Rich join associations6m 28s
-
(Locked)
Traversing a rich join association3m 38s
-
(Locked)
Joining tables during queries6m 51s
-
(Locked)
-