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.

named_scope and nested order clauses

#2253

named_scope currently has some unexpected behavior when nesting :order clauses. This problem also occurs in the underlying with_scope implementation.

This test does not pass:

def test_scoping_with_multiple_order
  expected = Monk.find(:all, :order => "birth_year, first_name").map{|m| m.full_name}
  options = {:order => "birth_year"}
  Monk.with_scope(:find => options) do
    assert_equal expected, Monk.find(:all, :order => "first_name").map{|m| m.full_name}
  end
end

This test passes, and has the expected behavior:

def test_single_named_scope_overrides_default_scope_order
  expected = Monk.find(:all, :order => "last_name asc").collect {|m| m.id}
  received = Monk.by_last_name.collect {|m| m.id}
  assert_equal expected, received
end

This test does not pass:

def test_nested_scopes_orders_combined
  expected = Monk.find(:all, :order => "last_name asc, first_name asc").collect {|m| m.last_name + ", " + m.first_name}
  received = Monk.by_last_name.by_first_name.collect {|m| m.last_name + ", " + m.first_name}
  assert_equal expected, received
end

I have included a patch containing these tests and the Monk model.

For more discussion see this conversation on Rails Core.

Reported by Erik Andrejko · March 16th, 2009 @ 01:35 PM

State: new
Milestone: 3.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Peter Wagenet
  2. Nicole
  3. Maurício Linhares
    Maurício Linhares

    On Rails 2.3.2 this isn't fixed yet.

    +1

    Would be really nice to have this fixed.

                    if key == :conditions && merge
                      if params[key].is_a?(Hash) && hash[method][key].is_a?(Hash)
                        hash[method][key] = merge_conditions(hash[method][key].deep_merge(params[key]))
                      else
                        hash[method][key] = merge_conditions(params[key], hash[method][key])
                      end
                    elsif key == :include && merge
                      hash[method][key] = merge_includes(hash[method][key], params[key]).uniq
                    elsif key == :joins && merge
                      hash[method][key] = merge_joins(params[key], hash[method][key])
                    elsif key == :order && merge
                      hash[method][key] = [params[key], hash[method][key]].join(' , ')
                    else
                      hash[method][key] = hash[method][key] || params[key]
                    end
    

    May 28th, 2009 @ 10:20 PM

  4. Erik Andrejko
    Erik Andrejko

    A patch that includes tests for the desired behavior is attached.

    June 7th, 2009 @ 12:23 AM

  5. Jon
    Jon
    • Assigned user set to Pratik

    Still having this problem as you can see in this gist: http://gist.github.com/136140

    Probably related to this ticket: #2346 named_scope doesn't override default_scope's :order key

    June 25th, 2009 @ 09:50 PM

  6. Pratik
    Pratik

    Hey,

    Can we have a patch without new tables please ?

    Thanks!

    July 3rd, 2009 @ 12:49 PM

  7. Emilio Tagua
    Emilio Tagua

    You may want to take a look to this ticket/patch, i think that solves this problem:

    https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets...

    July 7th, 2009 @ 09:43 PM

  8. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  9. bingbing