如何在squeel中动态处理where条件?
从这个输入:{‘hear’=> 1}我需要生成此查询
Score.joins(:target_disability).where{ (target_disabilities.name == 'hearing') & (round(total_score) >= 1) }
从这个输入{{hearing’=> 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 …
假设您的哈希值在my_params
:
@scores = Score.joins(:target_disability).where do my_params.map{|k,v| (target_disabilities.name==k) & (round(total_score)>=v) }.inject(:|) end