Filter list results

Hey All,

What’s the right way to make a list of something (games in my case)
but filtered by various parameters? For example, I have /game/list
showing all games, but if they specify /game/list/?
category=3&players=5 I want to show all games in that category that
allow 5 players.

Right now, I can get the category’s games by using

  if defined? params[:category]
    @games = Category.find(params[:category]).games;
  end

and I can get the players by using

  if params[:players].to_i > 0
    @games = Game.find(:all, :conditions => ['players = ?',

params[:players]]);
end

But how can I do both?

Any Ideas? I know how to do it with a straight SQL Query, but I’m not
sure if there is a more ruby-like way

Straight SQL isn’t bad for this kind of stuff… @games =
Game.find_by_sql("select *…)

But there are other ways too. Conditions can be built up
programatically…

See Ben Curtis’ well-written article on this approach:

http://www.bencurtis.com/archives/2008/06/restful-searching-in-rails/

Or you could use something like ez_where

http://opensvn.csie.org/ezra/rails/plugins/dev/ez_where/README.txt

Thanks for the help!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs