Ruby 2.7 beginless ranges in where ActiveRecord::QueryMethods#where hash condition.

When you use Ruby 2.7.0’s new beginless range feature in a hash argument to .where the results it does not match the behavior of using Float::INFINITY..n.

irb(main):030:0> Post.where(id: Float::INFINITY…3) Post Load (0.4ms) SELECT “posts”.* FROM “posts” WHERE “posts”.“id” <= $1 LIMIT $2 [[“id”, 3], [“LIMIT”, 11]]

irb(main):031:0> Post.where(id: …3) Post Load (0.4ms) SELECT “posts”.* FROM “posts” WHERE “posts”.“id” BETWEEN $1 AND $2 LIMIT $3 [[“id”, nil], [“id”, 3], [“LIMIT”, 11]] => #<ActiveRecord::Relation >

``

Neither does it match the behavior of endless ranges which where introduced in 2.6.0.

irb(main):032:0> Post.where(id: 3…) Post Load (0.3ms) SELECT “posts”.* FROM “posts” WHERE “posts”.“id” >= $1 LIMIT $2 [[“id”, 3], [“LIMIT”, 11]] => #<ActiveRecord::Relation >

``

On a beginless range begin is nil, on endless end is nil.

I’m trying to look into this and what could actually be done about it. I’m a bit puzzled at why the endless range works as RangeHandler just lops off the begin and end and passes it to:

predicate_builder.build_bind_attribute

``

Which checks if the value is infinite.

Where along in the chain of RangeHandler → QueryAttribute → ActiveModel::Attribute should this be handled?

Thanks Max, you don’t have to both open an issue Beginless range in where condition does not match behavior of endless range. · Issue #38777 · rails/rails · GitHub and start a thread on here.

This is in master, scheduled for Rails 6.1, currently.

fredag den 20. marts 2020 kl. 20.00.51 UTC+1 skrev Max Calabrese:

1 Like