Richard :
when 1
conditions = ["pourcentage_k > ?", pourc ]
when 2
conditions = ["pourcentage_k = ?", pourc ]
when 3
conditions = ["pourcentage_k < ?", pourc ]
end
end
elsif not region == 0
conditions = ["region_id = ?", region]
end
Ton code est pas immédiatement compréhensible, ya plein
de if.
Dans ton exemple, on pourrait le réécrire comme ça, si j’ai
bien compris ton code :
SIGNES = [nil] + %w(> = <)
region = params[:region].to_i
signe = params[:signe].to_i
pourc = params[:pourc].to_f
conditions, arguments = [], []
unless region == 0
conditions << ‘(region_id = ?)’
arguments << region
end
unless pourc == 0
conditions << “(pourcentage_k #{ SIGNES[signe] } ?)”
arguments << pourc
end
si d’autres paramètres…
même principe on rajoute une condition SQL au tableau condition et
un argument au tableau arguments
courts = if conditions.empty?
Court.find(:all)
else
Court.find(:all, :conditions => [conditions.join(’ AND '), *arguments]
)
end
Sinon autre solution et/ou source d’inspiration, le plugin
resultset-autofilter de fredix :
http://code.google.com/p/resultset-autofilter
overkill ou pas, selon ton problème réel.
Sinon le fait de prendre une collection d’enregistrements et
de la filtrer jusqu’à obtenir la collection voulue, c’est possible
mais cela consiste à faire le boulot par Ruby et non par le
sgbdr.
– Jean-François.
–
À la renverse.