ActiveRecord's `where.not` makes queries easier to read:
Gabe Berke-Williams January 22, 2014 rails Rails 4.0 introduced a helpful new method for ActiveRecord queries: where.not . It can make clunky queries easier to read. Usage This query: User.where.not(name: 'Gabe') is effectively the same as this: User.where('name != ?', 'Gabe') It's "effectively" the same because where.not has some extra juice: it will fully qualify the column name with the table name, continue to work if the table or column get aliased (during a left outer join clause with includes), and will continue to work if the database implementation is switched. I've usually seen it used for NOT NULL queries: # Old and busted # User.where('name IS NOT NULL') # New hotness User.where.not(name: nil) But it works with arrays too: # Without `where.not` # Something.where("name NOT IN ?", User.unverified.pluck(:name)) # With `where.not` Something.where.not(name: User.unve...