Ruby

Ruby

Made by DeepSource

Usage of SQL fragments in where query method RB-C1016

Anti-pattern
Minor
Autofix

Prefer passing conditions to where and where.not as a hash over using fragments of SQL.

where.not also provides some extra features: * It will fully qualify the column name with the table name * It will continue to work if the table or column get aliased (during a left outer join clause with includes) * It will continue to work if the database implementation is switched

Bad practice

User.where('name <> ?', 'Gabe')
User.where('name <> :name', name: 'Gabe')
User.where('name IS NOT NULL')
User.where('name NOT IN (?)', ['john', 'jane'])

Recommended

User.where.not(name: 'Gabe')
User.where.not(name: nil)
User.where.not(name: ['john', 'jane'])
User.where.not(users: { name: 'Gabe' })

References:

  1. ActiveRecord's where.not
  2. ActiveRecord Query Interface