2012-05-13 9 views
2

:私はこの入力から、このクエリどのようにsqueelでwhere条件を動的に処理するのですか?この入力から

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

を生成するために必要{ '聴覚' => 1} {=> 1、 'モビリティ' => 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 ...

答えて

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 
+0

あなたは私のためにこれを見ることができますか? http://stackoverflow.com/questions/14654010/squeel-and-rails-dynamic-where-clause –

関連する問題