Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

activerecord condition hashes should respect ruby range syntax

#255

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.

Reported by Alex Arnell · May 26th, 2008 @ 05:00 PM

State: duplicate
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Alex Arnell
    Alex Arnell

    updated ticket with proper git formatted patch

    May 26th, 2008 @ 07:00 PM

  2. Pratik
    Pratik
    • 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.

    June 17th, 2008 @ 05:28 PM

  3. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag set to activerecord, bugmash, edge, enhancement, patch

    August 3rd, 2009 @ 02:48 PM

  4. dira
    dira

    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)

    August 8th, 2009 @ 03:26 PM

  5. Pratik
    Pratik
    • State changed from incomplete to duplicate

    August 8th, 2009 @ 03:42 PM

  6. CancelProfileIsBroken
    CancelProfileIsBroken
    • Assigned user cleared.
    • Tag changed from activerecord, bugmash, edge, enhancement, patch to activerecord, edge, enhancement, patch

    August 8th, 2009 @ 11:05 PM