Scopes!

Gabriela Johnson
1 min readDec 6, 2020

Let’s talk about Scopes in Ruby on Rails.

With a Scope method, you are able to create and define queries inside of your rails model using the scope method! What goes into a scope method may be similar to what you put in for example, an index method. The only problem is, it’s beat for code to be as dry as possible, and not using scopes in these situations and create duplicate code that is unnecessary.

Scopes are used for one thing, and one thing only! They have a specific syntax , which presents a cleaner code. They are specific , so when you see one, you most likely won’t have to do any guessing since they are pretty straight forward and easy to spot in the midst of of the code.

But wait , it gets better! Scopes also have the ability to take in arguments , which only makes it more flexible! For example , if you want a default value , you can have a default value! If you want it to be filled out based on the data entered, you can do that!

Scopes overall are very useful and simple to use once you learn!

This Article is based off of a Ruby On Rails project I just completed and a scope method was one of the requirements. In the future I will definitely be taking further advantage of these methods to produce dryer and cleaner code that’s easy to understand!

--

--