On Fri, Apr 9, 2010 at 13:26, Joao Carlos <[email protected]> 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 [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-core?hl=en.