Today we will see a quick tip about scopes in Rails 4.
You may have been used to this kind of scopes with Rails 3 :
scope :active, where(active: true)
But in Rails 4, you need to give it a callable object like a Proc or a Lambda :
scope :active, -> { where(active: true) }
You can also pass it a parameter :
scope :things, -> (type) { where(type: type)}
You can still define class method if you prefer :
self.active
where(active: true)
end
Whatever way you chose, always keep in mind that scopes have to be chainable. Therefore, they need to return an ActiveRecord::Relation object.