# Metodi all e scoped

**URL:** <https://www.ruby-forum.com/t/metodi-all-e-scoped/201229>\
**Category:** IT\
**Created:** [January 3, 2011, 11:49am UTC](https://www.ruby-forum.com/t/metodi-all-e-scoped/201229 "2011-01-03T11:49:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 3, 2011, 11:49am UTC](https://www.ruby-forum.com/t/metodi-all-e-scoped/201229/1 "2011-01-03T11:49:52Z")

</div>

qual’e’ la differenza tra:

Sector.all  
e  
Sector.scoped

entrambi restituiscono i record nel medesimo ordine.

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [January 3, 2011, 6:58pm UTC](https://www.ruby-forum.com/t/metodi-all-e-scoped/201229/2 "2011-01-03T18:58:11Z")

</div>

2011/1/3 Mauro [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> qual’e’ la differenza tra:
> 
> Sector.all  
> e  
> Sector.scoped

scoped ti da un oggetto di tipo scope, che rappresenta una  
sottoselezione degli oggetti che puoi restringere come vuoi.

Per esempio, puoi fare un  
students = Student.scoped  
students.where(:year =\> 4)  
students.size # numero di studenti al quarto anno  
students.each do .. #roba con gli studenti del quarto anno

Ovviamente se non restringi la selezione equivalente a usare tutta  
la tabella, cio #all.

[http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html#method-i-scoped](http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html#method-i-scoped)

–  
blog en: [http://www.riffraff.info](http://www.riffraff.info)  
blog it: [http://riffraff.blogsome.com](http://riffraff.blogsome.com)  
work: [http://cascaad.com](http://cascaad.com)

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [January 3, 2011, 8:39pm UTC](https://www.ruby-forum.com/t/metodi-all-e-scoped/201229/3 "2011-01-03T20:39:54Z")

</div>

2011/1/3 gabriele renzi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Per esempio, puoi fare un  
> students = Student.scoped  
> students.where(:year =\> 4)  
> students.size # numero di studenti al quarto anno  
> students.each do .. #roba con gli studenti del quarto anno
> 
> Ovviamente se non restringi la selezione equivalente a usare tutta  
> la tabella, cio #all.

Per restringere la selezione intendi l’uso degli scope nel model?

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [January 3, 2011, 8:58pm UTC](https://www.ruby-forum.com/t/metodi-all-e-scoped/201229/4 "2011-01-03T20:58:06Z")

</div>

Un bel post a riguardo

> **[edgerails.info](http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly-named-scope/index.html)**
>
> This domain may be for sale!
