Last Updated: February 25, 2016
·
1.185K
· muszynskis

Rails: How to Override default_scope?

Problem

class Post
  default_scope order('created_at DESC')
end

How to select all posts with order e.g. updated_at ASC?

Solution

There are two ways to do it:

1 ) Post.unscoped.order('updated_at ASC') (read more)

2 ) Post.reorder('updated_at ASC') (read more)

2 Responses
Add your response

Fix typo uncoped -> unscoped. Also should be noted that default_scope is bad practice

over 1 year ago ·

Thanks for that!
Is it a bad practise? Well... I wouldn't say that. If someone is aware of its behaviour and feels comfortable with it, then why don't let him to use it?

over 1 year ago ·