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 - Ruby on Rails Tutorial
From the course: Ruby on Rails 5 Essential Training
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…
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)
ActiveRecord and ActiveRelation4m 32s
-
(Locked)
Model naming5m 52s
-
(Locked)
Model attributes2m 51s
-
(Locked)
The Rails console3m 17s
-
(Locked)
Create records4m 53s
-
(Locked)
Update records3m 16s
-
(Locked)
Delete records2m 29s
-
(Locked)
Find records5m 48s
-
(Locked)
Query methods: Conditions7m 39s
-
(Locked)
Query methods: Order, limit, and offset6m 29s
-
(Locked)
Named scopes8m 26s
-
(Locked)
-
-
-
-
-
-
-
-
-
-
-