Allow user to select named object using GET parameters
I would suggest my very awesome act_as_filter plugin designed to filter the results with a user using named_scopes.
http://github.com/tobyhede/acts_as_filter/tree/master
Eval is great to use - but make sure you check for accepted / expected values (do I often just plug some values into an array and check accept_values.include? (Parameter))
a source to share
In the example you give, I would be explicit and concatenate the scopes to create the requested request:
scope = Post
scope = scope.random if params[:scope] == 'random'
@posts = scope.find(:all, ...) # or paginate or whatever you need to do
If params [: scope] is not "random" this is the same as calling Post.find (), otherwise it does Post.random.find ()
From one of the other answers, it looks like find_by_filter will do the same for you.
Using this pattern, you can also combine multiple scopes into a query if you need to support things that weren't mutually exclusive for example
scope = scope.only_monsters if params[:just_monsters] == 1
scope = scope.limit(params[:limit].to_i) unless params[:limit].to_i.zero?
So GETting / posts? scope = random & just_monsters = 1 & limit = 5 will give you:
Post.random.just_monsters.limit(5).find(:all, ...)
a source to share