That’s not doing quite what you think…by Ben
I recently helped an intern at Hedgeye work through a problem with a database query. Because I’m working in a separate timezone, I ended up making suggestions through a GitHub pull request. We discussed and decided that what I wrote was self-contained enough that I should re-post so it can help others.
:conditions => ["event_type != ?", 'LOGIN'||'LOGOUT'],
I don’t think this is doing quite what you think…
'LOGIN' || 'LOGOUT' # => 'LOGIN'
So this turns into:
where event_type != 'LOGIN'
I’m guessing you meant to do:
where event_type != 'LOGIN' or event_type != 'LOGOUT'
But, believe it or not,
!=is a MySQL proprietary extension to SQL. It would probably be best to use something that’s a part of ANSI SQL:
where event_type <> 'LOGIN' or event_type <> 'LOGOUT' -- alternative: where event_type not in ('LOGIN', 'LOGOUT')
Because these are literals (not user-provided values), there’s no point in sanitization using
:conditions => "event_type not in ('LOGIN', 'LOGOUT')",