Rails scopes must return object relation - RoR developers’ notes

iRonin IT Team - Experts in software development
developers notes, rails

Scopes must return the ActiveRecord::Relation object.

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!

Author's Bio
iRonin IT Team

Experts in software development

We are a 100% remote team of software development experts, providing web & mobile application development and DevOps services for international clients.

Similar articles

Bulletproof your development with remote team augmentation

Read how
This page is best viewed in portrait mode
Our websites and web services use cookies. We use cookies and collected data to enhance your experience, provide additional communication channels, improve marketing materials and enhance our offer. IRONIN SP. Z O.O. SP. K. is committed to protecting all the data that we collect or process in any way, especially data of personal nature. By accepting these terms you agree to our usage of cookies and processing your data, according to our Privacy Policy, and you declare that your browser settings reflect your preferences. Read more You have the right to revoke this agreement at any time, based on the terms of our Privacy Policy. You can change cookies settings in your browser. If you do not agree with us using cookies and processing your data, please change your cookies settings in your web browser and reject these terms. You can find more information about cookies, your data privacy This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies. data processing, and your rights in our Privacy Policy.