ActiveRecord appends ‘AND (1=0)’ to end of queries

Rails will generate SQL like AND (1=0) when you query for a column whose value is in an empty array:

Child.where(id: [])

Intuitively, you’d expect that to generate SQL like SELECT * FROM children WHERE id IN (), but () isn’t actually valid SQL. Since no rows will match that query, though, Rails works around this by generating an equivalent query that also returns no rows:

SELECT * FROM children WHERE 1=0;

Leave a Comment

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