0
我有一個看起來像一個API ...Rails的查詢活動記錄由成對或運算
get(not_unique_id:, pairs: [])
...其中,對可以像[{ foo: 1, bar: 'a' }, { foo: 2, bar: 'b' }]
,我想編寫一個查詢,會發現所有條目對於給定的非唯一的ID,通過對過濾,即...
Thing.where(not_unique_id: not_unique_id)
# need help here -> .where("(foo = #{pairs.first[:foo]} AND bar = #{pairs.first[:bar]}) OR (foo = #{pairs.second[:foo]} AND bar = #{pairs.second[:bar]}) OR ...")
.map # or whatever else I want to do
我需要執行在我輸入對每個條目這foo
和bar
比較的方式,或運算一起返回所有結果它們具有不唯一的ID和值對中的一個。
更重要的是,如果這是在'Record'一個範圍,那麼你基本上可以刪除所有提及「再線「在你的答案。這減少到非常簡潔: 'conditions.inject(all){| scope,condition | scope.or(where(condition))}' – Glyoko
@Glyoko你不應該那樣做。如果你提供了一個類級別的範圍,你必須返回'conditions.inject(self){...}'。不要使用'all'來放棄已經應用到示波器的任何條件。 – meagar
哎呀,你說得對,應該是'自我'。謝謝@meagar。 – Glyoko