[ruby-it] Ricerche condizionate in base alle categorie

Andrea Campi andrea.campi a zephirworks.com
Lun 9 Feb 2009 12:37:19 CET


On 2/9/09 12:04 PM, Pietro Giorgianni wrote:
> 2009/2/9 Luigi Maresca<marluigi a libero.it>:
>
> non testato, ma dovrebbe funzionare:
>
> conds = { }
>
> conds[:category_id] = @selected_cat if @category != "tutte"
> conds[:nation_id] = @selected_nat if @nation != "tutte"
> conds[:author_id] = @selected_aut if @autor != "tutti"
>
> @books = Book.find(:all, :conditions =>  conds, :order =>  'title')
> _______________________________________________
> Ml mailing list
> Ml a lists.ruby-it.org
> http://lists.ruby-it.org/mailman/listinfo/ml
>    
Personalmente trovo pių leggibile:

conds = {}
conds.merge! {:category_id => @selected_cat} if @category != "tutte"
... e cosė via.

In alternativa, proprio ieri ho scoperto squirrel 
(http://www.thoughtbot.com/projects/squirrel/), che ti permette di scrivere:

@books = Book.find(:all) do
   category_id == @selected_cat if @category != "tutte"
   ...
   order_by title
end

Andrea


More information about the Ml mailing list