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

Skip to first unread message

Max Calabrese

Mar 20, 2020, 3:00:51 PM3/20/20
to Ruby on Rails: Core
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]]

(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.

(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: 


Which checks if the value is infinite.

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

Kasper Timm Hansen

Mar 22, 2020, 12:10:30 PM3/22/20
to Ruby on Rails: Core
Thanks Max, you don't have to both open an issue and start a thread on here.

This is in master, scheduled for Rails 6.1, currently.
Reply all
Reply to author
0 new messages