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.

Solution: Named scopes

Solution: Named scopes - Ruby on Rails Tutorial

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

Start my 1-month free trial

Solution: Named scopes

(lively music) - [Instructor] I hope you did well with your first challenge assignment, let me show you my solution. The first task was to define a scope for visible and invisible in the page model, and you can see here I've done that, you can either do that by rewriting them or by copying and pasting them from subject. The next task was to write a sorted named scope, so you can see I have scope, sorted, and then a lambda, and then in curly braces, I'm now using the order method, I'm going to order it by position that makes sense to me, that a page should be ordered by its position column, it's up to you what column you sort it by. In the user model, I also added a sorted method, but in that case, I sorted it by last name first, and then by first name, that made sense to me. If we had a username column, we might sort by that, but we don't, so it makes sense to sort it by last name, and then first name. And then in the…

Contents