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

Joining tables during queries - Ruby on Rails Tutorial

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

Start my 1-month free trial

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…

Contents