Product.where(['color = ?',nil])
gives SQL
SELECT "products".* FROM "products" WHERE (color = NULL)
rather than what I want, which is:
SELECT "products".* FROM "products" WHERE (color IS NULL)
(color IS NULL, not color = NULL)
Using the hash syntax:
Product.where(color: nil)
gives what I want
SELECT "products".* FROM "products" WHERE "products"."color" IS NULL
But using the hash syntax I believe loses the benefit of anti-SQL injection that the array syntax offers.
EDIT - I omit to say that the condition will be a variable and will sometimes, but not always be nil:
Product.where(['color IS ?',nil])
does what I ask, but
Product.where(['color IS ?','red'])
for example will of course then fail
You're mistaken, it provides the same SQL injection protection as the array syntax.
Rails is even smart enough to handle a list containing a
nil:The hash syntax is superior to the "array syntax" in every way except flexibility when you need something it just doesn't support, so most devs prefer it unless they're unable to use it.