How do you put multiple default scopes on on a model?
-
27-10-2019 - |
Question
I was wondering how do you have multiple default scopes (ordering) on a model for example I have a comments model that needs ordering by both date and approved:
default_scope :order => 'approved ASC', :order => 'date ASC'
So how do you have both of these ordering put on a model, so I first orders by approved, and then by date.
Cheers!
Solution
Here is the good syntax for ordering with several fields :
default_scope :order => 'approved ASC, date ASC'
OTHER TIPS
In Rails 4+ you can do:
default_scope -> { order(approved: :asc, date: :asc) }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow