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
  • 11,402
  • 16
  • 63
  • 99

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
  • 25,154
  • 5
  • 48
  • 51
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
  • 156,572
  • 25
  • 250
  • 300