2

从这个输入: {'hearing' => 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...

4

1 回答 1

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
于 2012-05-13T12:42:57.047 回答