Joshua Paling

I'm not the first person to recommend against default scopes.

Just wanted to throw one more warning in the pot. My previous convention was that I would ONLY use default_scope for ordering:

class Customer < ActiveRecord::Base
  default_scope -> { order('name ASC') }

Seems pretty harmless, right? Wrong.

I've spent the last few hours tracking down a bug. Turns out the cause is that when you do Customer.last, Rails orders by your default scope, if one exists. So, you don't get the last inserted record, you'd get (in this case) the last alphabetically ordered. You can work around it by using Customer.unscoped.last - but sooner or later, you will forget to do this, and it won't be fun.

Never, ever, ever use Rails default scopes.