method
unscoped
v3.0.9 -
Show latest stable
- Class:
ActiveRecord::Base
unscoped()public
Returns a scope for this class without taking into account the default_scope.
class Post < ActiveRecord::Base default_scope :published => true end Post.all # Fires "SELECT * FROM posts WHERE published = true" Post.unscoped.all # Fires "SELECT * FROM posts"
This method also accepts a block meaning that all queries inside the block will not use the default_scope:
Post.unscoped { limit(10) # Fires "SELECT * FROM posts LIMIT 10" }
It is recommended to use block form of unscoped because chaining unscoped with named_scope does not work. Assuming that published is a named_scope following two statements are same.
Post.unscoped.published Post.published
1Note
unscoped.all / unscoped.count
At least in console, doing unscoped.all or unscoped.count initially returns expected results but, after you've added new records outside of the default_scope those two calls seem to use some cached values.
Therefore it should always be used with the block (as they sort of imply in the doc). unscoped { all } and unscoped {count }