Ruby Journal

Override Ordering With ActiveRecord

| Comments

If your ActiveRecord models happen to have default ordering scope, you could override this ordering scope in queries by using reorder method.

Imagine we have a model like this:

1
2
3
class User < ActiveRecord::Base
  default_scope { order(:updated_at) }
end

All queries would include this scope by default. If you want to override this scope, you can apply reorder to the query chain:

1
query_chain.reorder(:order_index)

That’s it for today folks!

Comments