ruby - Rails scope ordered ascending not responding in view -
in model have:
default_scope -> { order(created_at: :desc) } scope :ascending, -> { order(created_at: :asc) }
in view:
<ol class="notices comments"> <%= render notice.comments.ascending %> </ol>
in model, when change default_scope -> { order(created_at: :desc) }
default_scope -> { order(created_at: :asc) }
, notices respond expected , display in ascending order instead of descending. however, when change scope :ascending, -> { order(created_at: :asc) }
scope :ascending, -> { order(created_at: :desc) }
, doesn't change anything. what's wrong code?
when call order
, adds component order clause. if have default scope , add ascending
scope, end order by:
order created_at desc, created_at asc
the created_at asc
ignored because unlikely there ties break created_at desc
ordering.
you want use reorder
in scope replace order by:
scope :ascending, -> { reorder(created_at: :asc) }
this assumes ordering in effect comes default scope can lead surprises if like:
model.order(:whatever).ascending
the order(:whatever)
removed scope.
i tend think default scopes bad idea because hide things you, doubly when default scope includes order adjustments. i'd drop default scope , make callers explicitly specify ordering.
Comments
Post a Comment