Check out my latest project called Rails UI 🎨

February 4, 2021

4 responses

How to use Scopes in Ruby on Rails

Scopes are used to assign complex ActiveRecord queries into customized methods using Ruby on Rails.

Inside your models, you can define a scope as a new method that returns a lambda function for calling queries you're probably used to using inside your controllers.

A use case for scopes is often for filtering by status, date, date ranges, ordering, groupings, and more. ActiveRecord already makes it quite easy to return rich data from your database. Combining those superpowers with scopes allow you to define more memorable queries that you and your team can harness through your Ruby on Rails application.

Here's a quick example of setting status on a Post class. As an example, let's assume we're creating a basic blog and we want some posts to be drafts while others are published. We can assign a status to each post which signifies this.

Examples

class Post < ApplicationRecord
  scope :published, -> { where(status: "Published") }
end

Then in our controller or views, we can return the newly scoped objects.

class PostsController < ApplicationController
  def index
      # only returns those Post objects that have a "Published" status
      @posts = Post.published
  end
end

More complex queries can be performed in the same manner.


class Post < ApplicationRecord
  scope :past_week, -> { where(created_at: Time.zone.now.at_beginning_of_week...Time.zone.now.at_end_of_week) }
end

This returns everything that was created for the current week.

Chaining

Scopes are extremely powerful due to their ability to be chained together. Think of them as chain links. Keep in mind Scopes should only ever be written to be chained.

class PostsController < ApplicationController
  def index
    # returns all published posts for the current week
    @posts = Post.published.past_week
  end
end

Creating new records

Scopes can also be used while creating/building a record.

Post.status.new.published #=> true

Further reading

Looking for more Ruby on Rails content?

Check out my collections dedicated to the framework:

Leave a reply

Sign in or Sign up to leave a response.

4 responses

noble.bed0111
Icons/flag
Icons/link diagonal

created an account to thank you for this clear explanation! thanks!

Question: is this Post.published.past_week (chaining scopes) interperted by rails as an AND statement? so the Post must be published AND in the last week or is it
any Post that is published OR is published in the past week?

Is there a way to be notified if somebody response on my comment?

And thanks a lot for all the amazing ruby content you have put out over the years!!!! Thank you Andy!!! God bless!

Est. reading time: 2 minutes
Stats: 9,330 views

Categories

Collection

Part of the Ruby on Rails collection