Rails模型:SQL OR AND关键字

时间:2016-09-18 10:19:19

标签: sql ruby-on-rails models

我使用rails,我想要一个类似的请求:

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 个答案:

答案 0 :(得分:1)

您可以这样做:

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