从此输入:{'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 ...
答案 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