From the course: Ruby on Rails 5 Essential Training

Unlock the full course today

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

Named scopes

Named scopes - Ruby on Rails Tutorial

From the course: Ruby on Rails 5 Essential Training

Start my 1-month free trial

Named scopes

- [Teacher] In this movie, we will learn how to use named scopes in our models. So far in this chapter, we've learned to create, update, delete, and find records, and we did it without adding any additional code to our model. We've been using the built-in methods provided by ActiveRecord and ActiveRelation. Rails gives us these features for free, simply by defining a model so that it inherits from ActiveRecord::Base, which most of our models will. You can also write your own query methods, which can use and combine the built-in query methods. We can design a query and then save it in our models as a named scope. Named scopes assign a name to an ActiveRelation query and then store them in a model. We can call named scopes just like we call ActiveRelation query methods, and we can even daisy-chain them together with other ActiveRelation query methods. And they can accept parameters, just like a method would. In Rails 5, we're gonna make sure that we define these named scopes using…

Contents