2015-12-15 36 views
-1

我有兩個模型,我在兩者之間進行匹配以確定它們是否符合某些標準。和 - 或在主動記錄中使用兩個備選方案,其中查詢

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 

這是直截了當的,只要MODEL2不接受a和b型model1s。 我可以只使用: model1s.where(model_type: 「A」)。其中( 「?率> =」 @ model2.a_min)

但我想它包含

一個查詢響應如果模型1爲A型,

model1.rate > model2.a_min 

如果是b型

model1.rate < Model2.b_min 

所以......

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

我想我需要寫類似:

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) 

任何想法?

回答

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 
    }  
) 

似乎要做的伎倆。我錯過了哈希周圍的大括號。

相關問題