This project is archived and is in readonly mode.
activerecord condition hashes should respect ruby range syntax
Reported by Alex Arnell | May 26th, 2008 @ 05:00 PM
Ruby has some explicit rules for Ranges,
a = 1..10 => 1 through 10
b = 1...10 => 1 through 9
but activerecord doesn't follow those rules when uses condition hashes. Take for example:
date_range = (2.weeks.ago.to_date)...(Date.today)
Post.find(:all, :conditions => { :published_at => date_range })
When I read this could I would expect that query to return all the posts that happened in the last two weeks prior to today. However this is not the case. Activerecord uses the BETWEEN sql syntax to handle ranges, which means that the above query actually runs equivalently to (min <= published_at AND published_at <= max).
Attached is a patch (including tests) that corrects this behavior.
Comments and changes to this ticket
-
Pratik June 17th, 2008 @ 05:28 PM
- Assigned user set to Pratik
- State changed from new to incomplete
- Milestone cleared.
I feel the check should be made in attribute_condition() method. I understand it might require a bit more refactoring, but that's worth it.
Thanks.
-
CancelProfileIsBroken August 3rd, 2009 @ 02:48 PM
- Tag set to activerecord, bugmash, edge, enhancement, patch
-
dira August 8th, 2009 @ 03:26 PM
not reproducible.
The functionality has already been implemented:
# activerecord/test/cases/finder_test def test_find_on_hash_conditions_with_end_exclusive_range assert_equal [1,2,3], Topic.find(:all, :conditions => { :id => 1..3 }).map(&:id).sort assert_equal [1,2], Topic.find(:all, :conditions => { :id => 1...3 }).map(&:id).sort assert_raise(ActiveRecord::RecordNotFound) { Topic.find(3, :conditions => { :id => 2...3 }) } end
(and the patch does not apply)
-
Pratik August 8th, 2009 @ 03:42 PM
- State changed from incomplete to duplicate
What dira said - this was fixed in http://github.com/rails/rails/commit/9991868d85b25da672bf119bfcbff2...
-
CancelProfileIsBroken August 8th, 2009 @ 11:05 PM
- Assigned user cleared.
- Tag changed from activerecord, bugmash, edge, enhancement, patch to activerecord, edge, enhancement, patch
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>