On Fri, Apr 9, 2010 at 13:26, Joao Carlos <m...@joao-carlos.com> wrote:

>
> filter = %w(with_votes without_votes).include?(params[:filter]) ?
> params[:filter].to_sym : :self
> Idea.published.send(filter).all


I wouldn't call this use case where it would be "extremely handy". Here is
better code for it:

    filters = Idea.scopes.keys & Array.wrap(params[:filter]).map(&:to_sym)

    records = filters.inject(Idea.published) { |model, name|
model.send(name) }.all

First bonus feature is that it knows about all your named scopes, you don't
have to specify them in the controller (business logic in your controller =
wrong).

Another bonus feature — multiple filters can be given:

    GET /resource?filter[]=foo&filter[]=bar

Third bonus feature is that we didn't need to extend the language with a
method that doesn't do absolutely anything.
(Even `tap` does something and is incredibly useful.)

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to rubyonrails-c...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-core+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to