Rails 个型号:SQL 个 OR AND 关键字
Rails models : SQL OR AND keywords
我使用 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
我怎样才能有括号?
你可以这样做:
Event.where('WHERE id = ? AND active = ? AND (current_state = ? OR current_state = ?)', 5, true, 0, 1)
我使用 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
我怎样才能有括号?
你可以这样做:
Event.where('WHERE id = ? AND active = ? AND (current_state = ? OR current_state = ?)', 5, true, 0, 1)