Scopes must return the
When it comes to the
scopes declaration in Rails Models, we need to remember about returning relation objects. It’s very important to not return objects themselves (with first/last/find or any other method), because in the case of an empty result, the scope will return all records. It’s strictly connected with the general rule for scopes: chaining. Scopes should always be chainable.
See the first response to ActiveRecord scope with first returns all records issue in Ruby on Rails source code repository.
Today’s Developer’ Notes were shared by Darek, Ruby on Rails developer. Stay tuned for more tips and tricks in the near future!