This project is archived and is in readonly mode.

#2253 new
Erik Andrejko

named_scope and nested order clauses

Reported by Erik Andrejko | March 16th, 2009 @ 01:35 PM | in 3.x

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.

Comments and changes to this ticket

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>

Referenced by

Pages