-1

I have two models where I'm performing a match between the two to work out whether they fit certain criteria.

Model 1 
model_type == "a" or "b"
rate == Integer

Model 2 

a == true or false
b == true or false
#This defines whether or not model 2 accepts a's or b's respectively

a_min == integer or nil
b_min == integer or nil
#This defines that if the Model1 is of type "A", then Model1.rate needs to be greater than model2.a_min

This is straightforward so long as model2 doesn't accept both a and b type model1s. I can just use: model1s.where(model_type:"a").where("rate >= ?", @model2.a_min)

But I want to get one query response which contains

If model 1 is of type a,

model1.rate > model2.a_min

If it's of type b

model1.rate < Model2.b_min

So...

model1.where(model_type:["a","b"]).where(.........)

I think I need to write something like:

model1s.where("(model_type = 'a' AND rate >= :a_min) OR (model_type = 'b' AND rate >= :b_min)", :a_min => @model2.a_min, :b_min => @model2.b_min)

Any ideas?

Carpela
  • 2,155
  • 1
  • 24
  • 55

1 Answers1

0
model1s.where(
  "(model_type = :a AND rate >= :a_min) OR (model_type = :b AND rate >= :b_min)",
  {
    :a => "a", :a_min => @model2.a_min, 
    :b => "b", :b_min => @model2.b_min
  }     
)

Seemed to do the trick. I was missing the curly brackets around the hash.

Carpela
  • 2,155
  • 1
  • 24
  • 55