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)
Written by Sebastian Muszyński
Related protips
2 Responses
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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#