You probably already realized that you can achieve your goal using reorder . So my theory is why reorder works, and except does not.
It is important that methods like order , where , except handled by ActiveRecord::Relation instances, while scope, for example. ordered from your example, delegated by an instance of ActiveRecord::Relation for your model class.
some_relation.order(:x) method simply returns a new copy of some_relation with :x added to its order_values list. Similarly, some_relation.except(:order) will return a copy of some_relation with empty order_values . As long as the chain of calls consists of such relationship methods, except works as we expected.
The scope method call, when the scope is implemented as a lambda return relation, ends with the union of the model relation returned by scoped with the relation returned by lambda:
scopes[name] = lambda do |*args| options = scope_options.is_a?(Proc) ? scope_options.call(*args) : scope_options relation = if options.is_a?(Hash) scoped.apply_finder_options(options) elsif options scoped.merge(options)
And this merge does not retain the effect of except if it was made only for one of the combined relationships. If we combine a and b , and b has no order, but a , then the result will still be in order. Now reorder works with him with a trick: he sets the special flag reorder_flag in relation, which controls how the merge carries order_values .
Here is my test sample. I use default_scope to enter order in Product#scoped . In your example, the order is probably entered in Level#scoped by association in Pie , which might look like has_many :levels, :order => 'position' Level#scoped has_many :levels, :order => 'position' .
class Product < ActiveRecord::Base default_scope order('id DESC') scope :random_order, lambda { r = order('random()') puts "from lambda: " + r.order_values.inspect r } end
As you can see, due to the fact that Product.scoped has an id DESC order, it appears as a result, despite the fact that it has been cleared of the relation returned by the scope.
Here is a list of links to relevant sources: