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?
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:
Instead of always writing this you could, however, do something like this.
If you're using Rails 4 or newer, do this instead:
This allows you to access your subscribers by simply doing this:
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
Scoping allows you to specify commonly-used queries(it can be considered as shortcut for long or most frequently used queries) which can be referenced as method calls on the association objects or models. With these scopes, you can use every method previously covered such as where, joins and includes. All scope methods will return an ActiveRecord::Relation object which will allow for further methods (such as other scopes) to be called on it.
To define a simple scope, we use the scope method inside the class, passing the query that we'd like to run when this scope is called:
This is exactly the same as defining a class method, and which you use is a matter of personal preference:
Please follow the following link for full description with example. I hope this will help you.
http://guides.rubyonrails.org/active_record_querying.html
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
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,
And you call
It translates to the below in SQL,
Example above is based on rails 4 syntax
Now imagine you :
You could get those particular classes of people by using a scope!
Now that wasn't so hard was it?