0

I have a rails model with the following scopes:

 default_scope order('created_at ASC')
 scope :published, order('created_at DESC').where(:draft=>false)

Unfortunately, the published scope won't order the entries in descending order.

Am I writing this scope wrong?

jay
  • 12,066
  • 16
  • 64
  • 103

2 Answers2

1

I believe the :published scope won't overwrite the default ordering unless you using reorder:

http://guides.rubyonrails.org/active_record_querying.html#reorder

Try

scope :published, where(:draft=>false).reorder('created_at DESC')
Alex Peattie
  • 26,633
  • 5
  • 50
  • 52
1

Your default scope will still fire; you can either .reorder, explicitly get Foo.unscoped.published, or use with_exclusive_scope.

See this SO question for more details, including another SO question with more info.

Community
  • 1
  • 1
Dave Newton
  • 158,873
  • 26
  • 254
  • 302