This seems like such an obvious idea that I''m having trouble believing I''m the first to think of it. Why not take ranges containing Float::INFINITY and translate them to the appropriate greater than or less than comparisons? Example: class Person scope :voters, -> { where(born_on: (-Float::INFINITY..18.years.ago)) } end This would generate something along the lines of "WHERE people.born_on <= ''1995-02-19''". A proof of concept implementation was easy to knock out: https://github.com/tpope/rails/commit/b98545a930546854ddf401edfaad4a3a4860aeff This seems like a intuitive, unobtrusive way to make some comparison operators available without dropping down to SQL. Tell me why I''m wrong. -- 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 rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
On Mon, Feb 18, 2013 at 10:58:05PM -0500, Tim Pope wrote:> This seems like such an obvious idea that I''m having trouble believing > I''m the first to think of it. Why not take ranges containing > Float::INFINITY and translate them to the appropriate greater than or > less than comparisons? Example: > > class Person > scope :voters, -> { where(born_on: (-Float::INFINITY..18.years.ago)) } > end > > This would generate something along the lines of "WHERE people.born_on > <= ''1995-02-19''". > > A proof of concept implementation was easy to knock out: > https://github.com/tpope/rails/commit/b98545a930546854ddf401edfaad4a3a4860aeff > > This seems like a intuitive, unobtrusive way to make some comparison > operators available without dropping down to SQL. Tell me why I''m > wrong.You''re wrong because you didn''t add any tests. ;-) -- Aaron Patterson http://tenderlovemaking.com/ -- 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 rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
On Tue, Feb 19, 2013 at 5:27 AM, Aaron Patterson <tenderlove@ruby-lang.org> wrote:> On Mon, Feb 18, 2013 at 10:58:05PM -0500, Tim Pope wrote: >> This seems like such an obvious idea that I''m having trouble believing >> I''m the first to think of it. Why not take ranges containing >> Float::INFINITY and translate them to the appropriate greater than or >> less than comparisons? Example: >> >> class Person >> scope :voters, -> { where(born_on: (-Float::INFINITY..18.years.ago)) } >> end >> >> This would generate something along the lines of "WHERE people.born_on >> <= ''1995-02-19''". >> >> A proof of concept implementation was easy to knock out: >> https://github.com/tpope/rails/commit/b98545a930546854ddf401edfaad4a3a4860aeff >> >> This seems like a intuitive, unobtrusive way to make some comparison >> operators available without dropping down to SQL. Tell me why I''m >> wrong. > > You''re wrong because you didn''t add any tests. ;-)Wanted to make sure I wasn''t crazy before sorting those out. I''ll take this as a vote of confidence. Testing led me to ARel as the more natural destination: https://github.com/tpope/arel/tree/infinity-ranges -- 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 rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
On Tue, Feb 19, 2013 at 11:51:16AM -0500, Tim Pope wrote:> On Tue, Feb 19, 2013 at 5:27 AM, Aaron Patterson > <tenderlove@ruby-lang.org> wrote: > > On Mon, Feb 18, 2013 at 10:58:05PM -0500, Tim Pope wrote: > >> This seems like such an obvious idea that I''m having trouble believing > >> I''m the first to think of it. Why not take ranges containing > >> Float::INFINITY and translate them to the appropriate greater than or > >> less than comparisons? Example: > >> > >> class Person > >> scope :voters, -> { where(born_on: (-Float::INFINITY..18.years.ago)) } > >> end > >> > >> This would generate something along the lines of "WHERE people.born_on > >> <= ''1995-02-19''". > >> > >> A proof of concept implementation was easy to knock out: > >> https://github.com/tpope/rails/commit/b98545a930546854ddf401edfaad4a3a4860aeff > >> > >> This seems like a intuitive, unobtrusive way to make some comparison > >> operators available without dropping down to SQL. Tell me why I''m > >> wrong. > > > > You''re wrong because you didn''t add any tests. ;-) > > Wanted to make sure I wasn''t crazy before sorting those out. I''ll take > this as a vote of confidence. > > Testing led me to ARel as the more natural destination: > > https://github.com/tpope/arel/tree/infinity-rangesExcellent. It seems legit. I wish there was a more elegant way than all the if / elsif, but I don''t see it. Send a PR and we can makeitso. -- Aaron Patterson http://tenderlovemaking.com/ -- 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 rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Rodrigo Rosenfeld Rosas
2013-Feb-19 22:02 UTC
Re: Float::INFINITY ranges in where() clause
Em 19-02-2013 18:10, Aaron Patterson escreveu:> On Tue, Feb 19, 2013 at 11:51:16AM -0500, Tim Pope wrote: >> On Tue, Feb 19, 2013 at 5:27 AM, Aaron Patterson >> <tenderlove@ruby-lang.org> wrote: >>> On Mon, Feb 18, 2013 at 10:58:05PM -0500, Tim Pope wrote: >>>> This seems like such an obvious idea that I''m having trouble believing >>>> I''m the first to think of it. Why not take ranges containing >>>> Float::INFINITY and translate them to the appropriate greater than or >>>> less than comparisons? Example: >>>> >>>> class Person >>>> scope :voters, -> { where(born_on: (-Float::INFINITY..18.years.ago)) } >>>> end >>>> >>>> This would generate something along the lines of "WHERE people.born_on >>>> <= ''1995-02-19''". >>>> >>>> A proof of concept implementation was easy to knock out: >>>> https://github.com/tpope/rails/commit/b98545a930546854ddf401edfaad4a3a4860aeff >>>> >>>> This seems like a intuitive, unobtrusive way to make some comparison >>>> operators available without dropping down to SQL. Tell me why I''m >>>> wrong. >>> You''re wrong because you didn''t add any tests. ;-) >> Wanted to make sure I wasn''t crazy before sorting those out. I''ll take >> this as a vote of confidence. >> >> Testing led me to ARel as the more natural destination: >> >> https://github.com/tpope/arel/tree/infinity-ranges > Excellent. It seems legit. I wish there was a more elegant way than > all the if / elsif, but I don''t see it.You mean like this? case when other.begin == -Float::INFINITY && other.end == Float::INFINITY then Nodes::NotIn.new self, [] when other.end == Float::INFINITY then Nodes::GreaterThanOrEqual.new(self, other.begin) when other.begin == -Float::INFINITY && other.exclude_end? then Nodes::LessThan.new(self, other.end) when other.begin == -Float::INFINITY then Nodes::LessThanOrEqual.new(self, other.end) when other.exclude_end? ... -- 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 rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.