Issue when retrieving records with empty array

In Rails 4 you can use User.where.not(id: []) which will give you the correct result. It produces:

SELECT "users".* FROM "users" WHERE (1 = 1)

Unfortunately User.where('id NOT IN (?)', []) should be equivalent but it is not. It still gives you the wrong result:

SELECT "users".* FROM "users" WHERE (id NOT IN (NULL))

References:

  • https://github.com/rails/rails/issues/778
  • https://github.com/rails/arel/commit/cbff1bcf
  • https://github.com/rails/rails/pull/8332
  • https://github.com/rails/rails/commit/8d02afeaee8993bd0fde69687fdd9bf30921e805

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)