This project is archived and is in readonly mode.
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
-
Vitalii Khustochka July 12th, 2010 @ 12:23 PM
- 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.
-
Vitalii Khustochka July 12th, 2010 @ 12:28 PM
- 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
-
Vitalii Khustochka July 12th, 2010 @ 12:31 PM
- no changes were found...
-
Vitalii Khustochka July 13th, 2010 @ 07:37 AM
- 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 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/b75fca9e57423eec81384ff3e35f38... -
Ryan Bigg October 9th, 2010 @ 10:00 PM
- Tag cleared.
- Importance changed from to Low
Automatic cleanup of spam.
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>
People watching this ticket
Attachments
Referenced by
- 5093 Impossible to overwrite the default scope ordering in the named scope using relation syntax (from [b75fca9e57423eec81384ff3e35f38fd4f7fe47f]) Added r...