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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Max Calabrese
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?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/057c43ae-7835-4580-a08f-fbe8fec65d8f%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

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

Kasper Timm Hansen
Thanks Max, you don't have to both open an issue https://github.com/rails/rails/issues/38777 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:
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?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/232cc7ab-14b1-4128-9a33-37d73e26df07%40googlegroups.com.