002. default_scope - cwy007/tips-and-skills GitHub Wiki
When you do not have any default_scope with order:
scope :ordered_by_title, -> { order(title: :asc) }
When you have a default_scope with order then you need to use reorder:
default_scope { order(created_at: :desc) }
scope :ordered_by_title, -> { reorder(title: :asc) }
or order with unscope:
default_scope { order(created_at: :desc) }
scope :ordered_by_title, -> { order(title: :asc).unscope(:order) }
The reorder method overrides the default scope order.
link:
https://stackoverflow.com/questions/29701265/how-to-build-scope-to-order-title