How to unscope a query from a default_scope in Rails

If you have a default_scope in your model and you want to make an unscoped query on that table:

YourModel.unscoped.where(:prop1 => 'someValue')

won’t work, the default_scope still be applied.

You need to call the reload method after unscoped. Like this:

YourModel.unscoped.reload.where(:prop1 => 'someValue')

Advertisements
Tagged ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: