如何在吱吱声中动态处理where条件?

时间:2012-05-13 11:34:33

标签: ruby-on-rails squeel

从此输入:{'hear'=> 1}我需要生成此查询

Score.joins(:target_disability).where{ (target_disabilities.name == 'hearing') & (round(total_score) >= 1) }

从这个输入是{'hear'=> 1,'mobility'=> 2},我需要生成这个:

Score.joins(:target_disability).where{ (target_disabilities.name == 'hearing') & (round(total_score) >= 1) | (target_disabilities.name == 'mobility') & (round(total_score) >= 2) }

等等......

如何概括?因为我的输入有时会有3或4个键...有时候1 ...

1 个答案:

答案 0 :(得分:2)

假设您的哈希值在my_params

@scores = Score.joins(:target_disability).where do
    my_params.map{|k,v| (target_disabilities.name==k) & (round(total_score)>=v) }.inject(:|)
end