On Sun, 2010-02-07 at 18:29 +0100, Paul Harrington wrote:
> 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.
----
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.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.