What's the significance of named scope in Rails?
Asked Answered
D

3

2

Before going for details.

Question 1:-- What's the meaning of scope here (ie named **scope)?**

what's the benefits of using named scope?

Now:-

from Agile Development with Rails book:--

class Order < ActiveRecord::Base
   named_scope :last_n_days, lambda { |days| {:conditions =>
  ['updated < ?' , days] } }

 named_scope :checks, :conditions => {:pay_type => :check}
end

Such a named scope would make finding the last week's worth of orders a snap.

   orders = Orders.last_n_days(7)

Scopes can also be combined

orders = Orders.checks.last_n_days(7)

why we are using named_scope here. We can do the same using methods. What's special thing we got using named_scope.

Disquieting answered 27/5, 2010 at 9:0 Comment(1)
Note for Rubyist like me ;-): named_scope is used in Rails<3, which is replaced by scope in Rails >=3.Sequent
I
2

Scope simply means some selected range. So if you use:

orders = Orders.checks.last_n_days(7)

then you want to select from orders only that orders that are payed with check and are within last 7 days. So you 'scope' orders.

Why not using methods?

Named scopes are methods. It is just a simpler way of definig them so you don't have to care about all details and you can be happy using it!

And remember that scopes are just adding some conditions (and other stuff) to sql query.

Irrefrangible answered 27/5, 2010 at 10:22 Comment(0)
L
3

we get shorter, chainable, and more readable code:

orders = Orders.checks.last_n_days(7)

is much more readable, shorter and not chainable than

orders = Orders.all :conditions => ["updated < ? and pay_type='check'", 7]

In Rails3 the advantage will be even greater, because of arel. For more information I recommend watching the Railscasts:

  1. 108 named_scope (some basics in rails 2)
  2. 202 Active Record Queries in Rails 3 (some basics in rails 3)
  3. 215 Advanced Queries in Rails 3 (some advanced topics in rails 3)
Lunalunacy answered 27/5, 2010 at 9:19 Comment(2)
Fahnenbruck Whats the meaning of scope here.What kind of scope we are talking about.Disquieting
What do you mean? IMHO answered this: we get shorter, chainable, and more readable code My Example Code is the same as yoursLunalunacy
T
2

The named_scope are really useful for 2 cases

better readabililty

With good named_scope you can understand more easily what you want really search.

Chaining

All named_scope can be chained. So if you want made a search system, it's easy to do. Made it before it was painful.

You can generate chain it on the fly.

Transhumance answered 27/5, 2010 at 9:17 Comment(0)
I
2

Scope simply means some selected range. So if you use:

orders = Orders.checks.last_n_days(7)

then you want to select from orders only that orders that are payed with check and are within last 7 days. So you 'scope' orders.

Why not using methods?

Named scopes are methods. It is just a simpler way of definig them so you don't have to care about all details and you can be happy using it!

And remember that scopes are just adding some conditions (and other stuff) to sql query.

Irrefrangible answered 27/5, 2010 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.