named_scope and params

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

Craig White 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.

Craig White 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? >

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.