Search code examples
sqlruby-on-railsmodels

Rails models : SQL OR AND keywords


I use rails and I want a request likes that:

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

I write something likes that:

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

But when I do that, I have this request:

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

How can I have parentheses?


Solution

  • You can do something like this:

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