What is scope/named_scope in rails?

Ruby on-RailsNamed Scope

Ruby on-Rails Problem Overview


I've recently started an internship. My employer uses ruby on rails, and I frequently encounter new syntax that I need to look up to understand. I've googled around for a good explanation of named_scope, but what I've found so far is mostly blog posts giving high praise for it, rather a straight definition or introduction.

What exactly is named_scope (now simply called scope) in ruby on rails?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

A scope is a subset of a collection. Sounds complicated? It isn't. Imagine this:

You have Users. Now, some of those Users are subscribed to your newsletter. You marked those who receive a newsletter by adding a field to the Users Database (user.subscribed_to_newsletter = true). Naturally, you sometimes want to get those Users who are subscribed to your newsletter.

You could, of course, always do this:

User.where(subscribed_to_newsletter: true).each do #something

Instead of always writing this you could, however, do something like this.

#File: users.rb
class User < ActiveRecord::Base
  scope :newsletter, where(subscribed_to_newsletter: true)
  #yada yada
end

If you're using Rails 4 or newer, do this instead:

#File: users.rb
class User < ActiveRecord::Base
  scope :newsletter, -> { where(subscribed_to_newsletter: true) }
  #yada yada
end

This allows you to access your subscribers by simply doing this:

User.newsletter.each do #something

This is a very simple example but in general scopes can be very powerful tools to easy your work.

Check out this link: API Description

Solution 2 - Ruby on-Rails

scope in active record is like class methods but they return Relation object which means you can call another scope or active record querying method on it.

For example, if you have a Zombie model (zombies table) with below mentioned scope methods,

class Zombie
  scope :rotting, -> { where(rotting: true) }
  scope :fresh, -> { where('age < ?', 25) }
  scope :recent, -> { order(created_at: :desc) }
end

And you call

Zombie.rotting.fresh.recent.limit(3)

It translates to the below in SQL,

select "zombies.*" from "zombies" where "zombies"."rotting" = 't' and (age<20) order by create_at desc limit 3

Example above is based on rails 4 syntax

Solution 3 - Ruby on-Rails

The best way to understand about the details is to go to API Documentation.

You'll get the complete details and the ways we can use Scopes.

API Documentation of Scope

Solution 4 - Ruby on-Rails

  • Imagine you have a model: Person.

Now imagine you :

  • want all the people in the world who have red hair.
  • want all the people in the world who play cricket

You could get those particular classes of people by using a scope!

Person.red_hair.cricket ## finds all people with red hair who play cricket
Person.red_hair ## finds all people with red hair
Person.cricket ## finds all people who play cricket.

Now that wasn't so hard was it?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZiggyView Question on Stackoverflow
Solution 1 - Ruby on-RailsMichael SchäfermeyerView Answer on Stackoverflow
Solution 2 - Ruby on-RailsAkshathaView Answer on Stackoverflow
Solution 3 - Ruby on-Rails123View Answer on Stackoverflow
Solution 4 - Ruby on-RailsBenKoshyView Answer on Stackoverflow