2016-09-18 52 views
0

我使用的軌道,我想請求喜歡的是:Rails模型:SQL或和關鍵字

SELECT * FROM events WHERE id = 5 AND 
active = true AND 
(current_state = 0 OR current_state = 1) 

我寫的東西喜歡的是:

Event.where(id:5).where(active:true).or(Event.where(current_state:)).or 
(Event.where(current_state: 1)) 

但是,當我這樣做,我有這個請求:

SELECT * FROM events WHERE id = 5 AND 
active = true AND 
current_state = 0 OR current_state = 1 

我該如何使用括號?

回答

1

你可以做這樣的事情:

Event.where('WHERE id = ? AND active = ? AND (current_state = ? OR current_state = ?)', 5, true, 0, 1) 
+0

謝謝!對於喜歡,我可以做'Event.where(「city like:city1」,{city1:「%#{params [:party] [:city]}%」})'? – Alexandre

+0

我不確定你的方法,但它會工作'Event.where(「city LIKE:?」,「%#{params [:party] [:city]}%」)'' –