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.

Impossible to overwrite the default scope ordering in the named scope using relation syntax

#5093

Assuming we have

  class DeveloperOrderedBySalary < ActiveRecord::Base
    self.table_name = 'developers'
    default_scope :order => 'salary DESC'
    scope :by_name, :order => 'name DESC'
  end

Calling DeveloperOrderedBySalary.by_name will overwrite the default scope order (there is a test for this: test_named_scope_overwrites_default).

But if we use the relation syntax

  class DeveloperOrderedBySalary < ActiveRecord::Base
    self.table_name = 'developers'
    default_scope order('salary DESC')
    scope :by_name, order('name DESC')
  end

DeveloperOrderedBySalary.by_name will not overwrite the ordering, but will concatenate it. I guess this is expected behavior according to https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets...

But then I expect reorder to work in the named scope:

    scope :by_name, reorder('name DESC')

but this fails with undefined method reorder'</code> exception. This is because reorder is not delegated in ActiveRecord::Base like 'order' and other relation methods.

Reported by Vitalii Khustochka · July 12th, 2010 @ 12:15 PM

State: resolved
Milestone: none
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Vitalii Khustochka
    Vitalii Khustochka
    • Tag set to arel rails3, order, patch, scope

    Thus I have added reorder to ActiveRecord::Base delegations. Also I have added the test.

    Please review the patch and comment if I have done something the wrong way. I don't know exactly where to add the test, so I modified the existing model DeveloperOrderedBySalary. Maybe I should have created new model to test named scopes ordering specifically.

    July 12th, 2010 @ 12:23 PM

  2. Vitalii Khustochka
    Vitalii Khustochka
    • Title changed from Impossible to overwrite the default scope ordering in the named scope to Impossible to overwrite the default scope ordering in the named scope using relation syntax

    July 12th, 2010 @ 12:28 PM

  3. Vitalii Khustochka
  4. Vitalii Khustochka
    Vitalii Khustochka
    • State changed from new to resolved

    (from [b75fca9e57423eec81384ff3e35f38fd4f7fe47f]) Added reorder delegation for ActiveRecord::Base(to be able to overwrite the default_scope ordering in the named scope [#5093 Impossible to overwrite the default scope ordering in the named scope using r... state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/b75fca9e57423eec81384ff3e35f38...

    July 13th, 2010 @ 07:37 AM

  5. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 9th, 2010 @ 10:00 PM

  6. bingbing