Rails 4 default scope

Joe Gatt picture Joe Gatt · Aug 29, 2013 · Viewed 52k times · Source

In my Rails app have a default scope that looks like this:

default_scope order: 'external_updated_at DESC'

I have now upgraded to Rails 4 and, of course, I get the following deprecation warning "Calling #scope or #default_scope with a hash is deprecated. Please use a lambda containing a scope.". I have successfully converted my other scopes but I don't know what the syntax for default_scope should be. This doesn't work:

default_scope, -> { order: 'external_updated_at' }

Answer

Luke picture Luke · Aug 29, 2013

Should be only:

class Ticket < ActiveRecord::Base
  default_scope -> { order(:external_updated_at) } 
end

default_scope accept a block, lambda is necessary for scope(), because there are 2 parameters, name and block:

class Shirt < ActiveRecord::Base
  scope :red, -> { where(color: 'red') }
end