Saturday, October 9, 2010

Re: [Rails] Re: How to pass values to named scope?

Hi Colin, yes something like what you've proposed would work. This is the most concise syntax I could come up with to get your suggestion working.

Media.scoped(:conditions => (name.blank? ? {:name => name.downcase!} : {}))

My suggestion (below) was to illustrate that you could have something that looks like a named scope (dvds) actually be a method. At some point the logic you need to apply may not fit nicely into a single named_scope call and I find this to be a good way to handle these situations.

Luke


On 2010-10-09, at 2:28 PM, Colin Law wrote:

> On 9 October 2010 00:44, Luke Cowell <lcowell@gmail.com> wrote:
>> Sometimes if I need to do a calculation before I call a named scope or if I want to make a different query based on the input, I do something like this:
>>
>> class Media < AR:Base
>> def dvds(name = "")
>> if name.blank?
>> Media.scoped({})
>> else
>> name.downcase!
>> Media.scoped(:conditions => {:name => name})
>> end
>> end
>> end
>
> Could that just be
> Media.scoped( :conditions => {:name => name.downcase!} unless name.blank? )
> or something similar, I have not tried it.
>
> Colin
>
> --
> 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 rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate