168

If I have an ActiveRecord::Base model with a default-scope:

class Foo < ActiveRecord::Base

  default_scope :conditions => ["bar = ?",bar]

end

Is there any way to do a Foo.find without using the default_scope conditions? In other words, can you override a default scope?

I would have thought that using 'default' in the name would suggest that it was overridable, otherwise it would be called something like global_scope, right?

1

9 Answers 9

220

In Rails 3:

foos = Foo.unscoped.where(:baz => baz)
5
  • 65
    This has a side effect, if Post has_many Comment, Post.first.comments.unscoped returns ALL comments. Sep 5, 2011 at 16:28
  • 3
    This really screwed me up for a while. Especially if you end up putting this in a class method like: def self.random; unscoped.order('rand()'); end unscoped removes ALL sql before it, not just what is listed under default_scope. While technically a correct answer, be careful using unstopped
    – Schneems
    Dec 25, 2011 at 21:58
  • 8
    WARNING! Unscoped does NOT remove the default_scope only, it was already said in another comment but it can really mess up with things.
    – dsimard
    Feb 29, 2012 at 14:42
  • 19
    A good rule of thumb is to only unscoped when it can directly follow a model, e.g. Foo.unscoped.blah() is ok but never Foo.blah().unscoped. Aug 1, 2013 at 22:23
  • stackoverflow.com/questions/1834159/… works around the side effect mentioned by Enrico
    – wbharding
    Sep 27, 2019 at 23:18
162

Short answer: Do not use default_scope unless you really have to. You'll probably be better off with named scopes. With that said, you can use with_exclusive_scope to override the default scope if you need to.

Have a look at this question for more details.

9
  • 11
    > Don't use default_scope unless you really have to. An excellent advise! Thank you!
    – installero
    Feb 1, 2013 at 10:02
  • 3
    So true. Using default_scope might seem like a good idea, but will likely cause multiple headaches during the lifetime of your app.
    – thomax
    May 8, 2014 at 7:46
  • 7
  • 7
    You are exaggerating a little sir. default_scope is an excellent tool and there are situations where you could another way but default_scope its just the right thing to do. For example, when you have a Product model that has a inactive flag, setting a default_scope { where inactive: false } is the best thing to do, since 99% or cases you will not want to display a inactive product. Then you just call unscoped on the remaining 1% cases, which is probably a Admin panel.
    – pedrozath
    Jan 27, 2017 at 9:53
  • 1
    Default scope violates the principle of least astonishment. I'm busy cursing previous developers for using this! Apr 26, 2019 at 15:33
117

If all you need is to change the order defined in default_scope, you can use the reorder method.

class Foo < ActiveRecord::Base
  default_scope order('created_at desc')
end

Foo.reorder('created_at asc')

runs the following SQL:

SELECT * FROM "foos" ORDER BY created_at asc
2
  • 4
    Tip: define a scope like scope :without_default_order, -> { reorder("") } and you can do things like Foo.without_default_order.order("created_at ASC") In some situations it reads better (maybe not this exact situation, but I had one).
    – Henrik N
    Jul 1, 2015 at 13:49
  • Reorder did it for me. Thanks a lot! Sep 23, 2019 at 13:09
55

Since 4.1 you can use ActiveRecord::QueryMethods#unscope to fight default scope:

class User < ActiveRecord::Base
  default_scope { where tester: false }
  scope :testers, -> { unscope(:where).where tester: true }
  scope :with_testers, -> { unscope(:where).where tester: [true, false] }
  # ...
end

It is currently possible to unscope stuff like: :where, :select, :group, :order, :lock, :limit, :offset, :joins, :includes, :from, :readonly, :having.

But still please avoid using of default_scope if you can. It's for your own good.

1
  • This answer should be higher Nov 30, 2017 at 21:17
13

You can override a default scope using the with_exclusive_scope method. So:

foos = Foo.with_exclusive_scope { :conditions => ["baz = ?", baz] }
2
11

On Rails 5.1+ (and maybe earlier, but I've tested it works on 5.1) it is possible to unscope a specific column, which imho is the ideal solution for removing a default_scope in a fashion that can be used inside a named scope. In the case of the OPs default_scope,

Foo.unscope(where: :bar)

Or

scope :not_default, -> { unscope(where: :bar) }
Foo.not_default

Will both result in a sql query that doesn't apply the original scope, but does apply whatever other conditions get merged into the arel.

5

Rails 3 default_scope does not appear to get overridden like it did in Rails 2.

e.g.

class Foo < ActiveRecord::Base
  belongs_to :bar
  default_scope :order=>"created_at desc"
end

class Bar < ActiveRecord::Base
  has_many :foos
end

> Bar.foos
  SELECT * from Foo where bar_id = 2 order by "created_at desc";
> Bar.unscoped.foos
  SELECT * from Foo;  (WRONG!  removes the "has" relationship)
> Bar.foos( :order=>"created_at asc" )  # trying to override ordering
  SELECT * from Foo where bar_id = 2 order by "created_at desc, created_at asc"

In my app, using PostgreSQL, the ordering in the default scope WINS. I'm removing all of my default_scopes and coding it in explicitly everywhere.

Pitfall Rails3!

1
  • 1
    You have to use Bar.foos.reorder(:created_at => :asc)
    – Ivan Stana
    Feb 23, 2013 at 16:41
4

With Rails 3+ you can use a combination of unscoped and merge:

# model User has a default scope
query = User.where(email: "[email protected]")

# get rid of default scope and then merge the conditions
query = query.unscoped.merge(query)
1
  • This also worked for me, to call unscoped first (Rails 4.2): User.unscoped.where(email: "[email protected]")
    – Nick B
    May 17, 2017 at 4:55
2

Well, you can always use the old time favorite find_by_sql with the complete query. For example: Model.find_by_sql("SELECT * FROM models WHERE id=123")

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.