# Named\_scope and params

**URL:** <https://www.ruby-forum.com/t/named-scope-and-params/183078>\
**Category:** Rails\
**Created:** [February 7, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/named-scope-and-params/183078 "2010-02-07T18:05:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 7, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/named-scope-and-params/183078/1 "2010-02-07T18:05:33Z")

</div>

I almost suspect that I can’t do this because it hasn’t yet worked for  
me…

My Salon model…

this works…  
named\_scope :states,  
:group =\> ‘state’,  
:select =\> ‘state’

doesn’t seem to work (at least not in script/console)  
named\_scope :salon\_cities\_by\_state,  
:conditions =\> {:state =\> @state},  
:group =\> ‘city’,  
:select =\> ‘city’

in script/console…

@state = “AZ”  
=\> “AZ”

Salon.states  
=\> [#\<Salon state: “AZ”\>, #\<Salon state: “CA”\>, #\<Salon state: “CT”\>,  
#\<Salon state: “NV”\>, #\<Salon state: “NY”\>, #\<Salon state: “TX”\>]

Salon.salon\_cities\_by\_state  
=\> []

But if I manually execute the ‘named\_scope’ in script/console, it  
works…

Salon.find(:all, :conditions =\> {:state =\> @state}, :group =\>  
‘city’, :select =\> ‘city’)  
=\> [#\<Salon city: “Chandler”\>, #\<Salon city: “Flagstaff”\>, #\<Salon city:  
“Glendale”\>, #\<Salon city: “Mesa”\>, #\<Salon city: “Phoenix”\>, #\<Salon  
city: “Prescott”\>, #\<Salon city: “Scottsdale”\>, #\<Salon city: “Sun  
City”\>, #\<Salon city: “Tempe”\>, #\<Salon city: “Tucson”\>]

Is there a way I can pass a param to a named\_scope so I can build my  
dynamically array in any view for any model?

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 7, 2010, 6:29pm UTC](https://www.ruby-forum.com/t/named-scope-and-params/183078/2 "2010-02-07T18:29:30Z")

</div>

Craig W. wrote:

> I almost suspect that I can’t do this because it hasn’t yet worked for  
> me…
> 
> My Salon model…
> 
> this works…  
> named\_scope :states,  
> :group =\> ‘state’,  
> :select =\> ‘state’
> 
> doesn’t seem to work (at least not in script/console)  
> named\_scope :salon\_cities\_by\_state,  
> :conditions =\> {:state =\> @state},  
> :group =\> ‘city’,  
> :select =\> ‘city’
> 
> in script/console…
> 
> @state = “AZ”  
> =\> “AZ”
> 
> Salon.states  
> =\> [#\<Salon state: “AZ”\>, #\<Salon state: “CA”\>, #\<Salon state: “CT”\>,  
> #\<Salon state: “NV”\>, #\<Salon state: “NY”\>, #\<Salon state: “TX”\>]
> 
> Salon.salon\_cities\_by\_state  
> =\> []
> 
> But if I manually execute the ‘named\_scope’ in script/console, it  
> works…
> 
> Salon.find(:all, :conditions =\> {:state =\> @state}, :group =\>  
> ‘city’, :select =\> ‘city’)  
> =\> [#\<Salon city: “Chandler”\>, #\<Salon city: “Flagstaff”\>, #\<Salon city:  
> “Glendale”\>, #\<Salon city: “Mesa”\>, #\<Salon city: “Phoenix”\>, #\<Salon  
> city: “Prescott”\>, #\<Salon city: “Scottsdale”\>, #\<Salon city: “Sun  
> City”\>, #\<Salon city: “Tempe”\>, #\<Salon city: “Tucson”\>]
> 
> Is there a way I can pass a param to a named\_scope so I can build my  
> dynamically array in any view for any model?
> 
> Craig
> 
> –  
> This message has been scanned for viruses and  
> dangerous content by MailScanner, and is  
> believed to be clean.

Pass a proc to named\_scope.

named\_scope :salon\_cities\_by\_state, lambda {|state|  
{ :conditions =\> { :state =\> state }, :group =\> “city”, :select =\>  
“city” } }

Although this is not a good usage of named\_scope. Just use a normal  
class method.

Named scopes do just that: create _scopes_ from which you can construct  
and chain additional queries on the object. A named\_scope which returns  
a collection containing only one property of the object is a rather  
useless “scope”; just use a plain old class methods if you need to do  
this and be done with it.

Also, please don’t build arrays in views. For simple retrieval do this  
in a controller; for more complex retrieval/data organization put this  
in the appropriate model, or create a new class/module. Easier to  
maintain, easier to cache results, easier to test, etc etc.

Perhaps more importantly; I see alot of posts of yours dealing with  
indirect ways of working with your Cities and States. Do you have City  
and State ActiveRecord models? If so, do you have the appropriate  
relations set up with your other models? It seems like simply doing this  
would have eliminated all the related problems you’ve encountered.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 7, 2010, 9:25pm UTC](https://www.ruby-forum.com/t/named-scope-and-params/183078/3 "2010-02-07T21:25:09Z")

</div>

On Sun, 2010-02-07 at 18:29 +0100, Paul H. wrote:

> > Salon.find(:all, :conditions =\> {:state =\> @state}, :group =\>  
> > ‘city’, :select =\> ‘city’)  
> > =\> [#\<Salon city: “Chandler”\>, #\<Salon city: “Flagstaff”\>, #\<Salon city:  
> > “Glendale”\>, #\<Salon city: “Mesa”\>, #\<Salon city: “Phoenix”\>, #\<Salon  
> > city: “Prescott”\>, #\<Salon city: “Scottsdale”\>, #\<Salon city: “Sun  
> > City”\>, #\<Salon city: “Tempe”\>, #\<Salon city: “Tucson”\>]
> > 
> > Is there a way I can pass a param to a named\_scope so I can build my  
> > dynamically array in any view for any model?

> and chain additional queries on the object. A named\_scope which returns  
> indirect ways of working with your Cities and States. Do you have City  
> and State ActiveRecord models? If so, do you have the appropriate  
> relations set up with your other models? It seems like simply doing this  
> would have eliminated all the related problems you’ve encountered.

* * *

thanks for the proc method - that clearly worked.

FTR… I am definitely not trying to build arrays in the views, that is  
why I was playing with scopes in the model. It’s just that I am trying  
to build my array of Salon.cities and Salon.states using scopes because  
I couldn’t figure out an adequate method that I could use in my salon  
model to get the list of states or a list of cities for a particular  
state or a list of salons in a particular city/state that we had salons  
in for my selector in ‘Reviews’ views. Your proc method has delivered  
the goods. But if you want to clue me on a ‘method’ that I can use in my  
salon.rb that will give me an array of states (grouped by state), then I  
will surely use it instead of a named scope.

No, I do not have City or State ActiveRecord models at all. I didn’t see  
any need to drive that path. In essence, they are sort of automatically  
derived from GeoKit by at least the Zip Code but I am almost at the  
point of requiring a ‘street’ level precision before a new Salon can be  
added and GeoKit can easily supply the ‘City’ and ‘State’ information…  
no typos 😉

Thanks

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.
