Building on the fact, which you've discovered, that [1..5]
is not the correct way to specify the range... I have discovered why [1..5]
behaves as it does. To get there, I first found that an empty array in a hash condition produces the 1=0
SQL condition:
User.where(id: []).to_sql
# => "SELECT "users".* FROM "users" WHERE 1=0"
And, if you check the ActiveRecord::PredicateBuilder::ArrayHandler code, you'll see that array values are always partitioned into ranges and other values.
ranges, values = values.partition { |v| v.is_a?(Range) }
This explains why you don't see the 1=0
when using non-range values. That is, the only way to get 1=0
from an array without including a range is to supply an empty array, which yields the 1=0
condition, as shown above. And when all the array has in it is a range you're going to get the range conditions (ranges
) and, separately, an empty array condition (values
) executed. My guess is that there isn't a good reason for this... it just simply is easier to let this be than to avoid it (since the result set is equivalent either way). If the partition code was a bit smarter then it wouldn't have to tack on the additional, empty values
array and could skip the 1=0
condition.
As for where the 1=0
comes from in the first place... I think that comes from the database adapter, but I couldn't find exactly where. However, I would call it an attempt to fail to find a record. In other words, WHERE 1=0
isn't ever going to return any users, which makes sense over alternative SQL like WHERE id=null
which will find any users whose id is null (realizing that this isn't really correct SQL syntax). And this is what I'd expect when attempting to find all Users whose id is in the empty set (i.e. we're not asking for nil ids or null ids or whatever). So, in my mind, leaving the bit about exactly where 1=0
comes from as a black box is OK. At least we now can reason about why the range inside of the array is causing it to show up!
UPDATE
I've also found that, even when using ARel directly, you can still get 1=0
:
User.arel_table[:id].in([]).to_sql
# => "1=0"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…