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

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Named scopes

Named scopes - Ruby on Rails Tutorial

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

Start my 1-month free trial

Named scopes

- In this movie, we're going to learn how to use Named Scopes in our models. So far, we've been using the built-in methods provided by active record and active relation in order to query our database. Named Scopes, allows us to write custom queries in our models. They're still defined using those same query methods we've been learning, but we can group them together in ways that we like, and then give them a convenient name to call them. You still call them like ActiveRelation query methods? And you can even chain them together and mix and match. Let me give you an example. Here I have a class called product and it has a name scope on it. You'll notice that the syntax is scope followed by the name that we want to give it as a symbol. Here, the name is active, then a comma, and then something called a Lambda. Lambdas are a more advanced part of Ruby and you aren't going to come in contact with them very often. This is…

Contents