This project is archived and is in readonly mode.

#5093 ✓resolved
Vitalii Khustochka

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

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

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.

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>

Attachments

Referenced by

Pages