2

From this input: {'hearing' => 1} I need to generate this query

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

From this input is {'hearing' => 1, 'mobility' => 2}, I need to generate this:

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

And so on...

How can this be generalized? Because my input sometimes has 3 or 4 keys... sometime 1...

Boti
  • 3,275
  • 1
  • 29
  • 54

1 Answers1

2

Assuming your hash is in my_params:

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