Learning to Use Scopes in Ruby on Rails

Suppose you have a Movie model that includes all the movies you want to display on your website, but showing them all at once isn't very useful for your users. They want to be able to filter them by categories, release date, country of origin, or by the latest additions.

In this article, I'll show you how to improve your website using the method provided by Ruby on Rails called scope.

Your table would look something like this—though you could definitely include more fields and even external relationships, like linking to an artists table. But hey, let’s not overcomplicate things right now! So, for now, our schema.rb would look a little like this:



Solution #1 (Creating a Helper)

Create a Helper with methods that execute the query we want in our Movies model:


Solution #2 (Creating Methods in the Model)

Create the same methods in the Model. Note that in the Model you must use the reserved word self before the method name because we want it to be a class method, with this way we can access it like this: Movie.filter_by_category('action'). Since it's a class method, we don't need to use the Model name in our query, that's why we use where directly...


Understanding the Problem...

If you're only going to use one or two filters in your model, the solutions presented above would be fine, but imagine if you had to apply 20 filters or more? Do you see what the problem is? Your model would be full of methods to filter the database, which would make your code difficult to read, disorganized, and unprofessional. You won't get hired that way, man! 🤬


Let's welcome Scopes:

Ruby on Rails is a super complete framework and provides a method called scopes. This method allows you to create database queries according to certain rules that you define to later use them directly in your model.


Scopes

All you have to do is use the method at the beginning of your model and put the query you want to execute inside a lambda:

WTF? What is this??? 🤯🤢, relax, look, it's a Rails method, imagine it like this:

Now you get it? 😉



Basic example:


Basic Scope

This scope doesn't receive any arguments:

This will return all movies ordered from the latest added or newest. For example:


Scope with Parameters

This scope receives one or more arguments and performs the query:

This will return all movies filtered by the action category. For example:


Default Scope

This is a scope that will be used in absolutely all your queries. For example, if you wanted the movies to be ordered in descending order, you could use something like this:

❗Be careful, because this will execute in all your queries and may result in unexpected outputs.


Final Result

The final result of our scope implementation would look like this:

Cleaner, clearer, and more professional, right?😏


Conclusion

Now that you know how to use scopes, you're one step further in your career as a Ruby on Rails professional developer. Don't forget that the key is to keep learning new things every day.

Good luck in your professional growth but most importantly in your spiritual growth.

Post a new comment. It's cool!

Developed in Bolivia 🇧🇴 by Marcelo Alarcón