2013-08-31 51 views
0

我有一個下面的查詢,它根據訂單優先級給出記錄。我想將這個傳統的mySql查詢翻譯成rails ORM,但我無法做到這一點。
我的查詢如下:
如何將mySql查詢轉換爲rails ORM?

SELECT * 
FROM user_questions q 
WHERE question_sequence_number IN (11,13,16,19) 
OR id IN (198,199,200,201,202) 
ORDER BY (question_sequence_number IN (11,13,16,19)) DESC, 
     CASE WHEN question_sequence_number IN (11,13,16,19) 
      THEN question_sequence_number 
      ELSE id 
     END 

任何人可以請幫我在這?

回答

0

最終我只能自己寫。上述查詢將被轉換成在ORM導軌是如下:

UserQuestion.where("question_sequence_number IN (?) OR id IN (?)", [11,13,16,19], [198,199,200,201,202]).order("question_sequence_number IN (#{[11,13,16,19].join(',')}) DESC, CASE WHEN question_sequence_number IN (#{[11,13,16,19].join(',')}) THEN question_sequence_number ELSE id END") 

感謝。