3

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!

Bohdan
  • 8,298
  • 6
  • 41
  • 51
CafeHey
  • 5,699
  • 19
  • 82
  • 145

2 Answers2

8

In Rails 4+ you can do:

default_scope -> { order(approved: :asc, date: :asc) }
milkman
  • 476
  • 9
  • 11
6

Here is the good syntax for ordering with several fields :

default_scope :order => 'approved ASC, date ASC'
Adrien Coquio
  • 4,870
  • 2
  • 24
  • 37