Finders should return ActiveRecord collections
On GitLab codebase, finders (placed under app/finders
or ee/app/finders
) are used to retrieve data from database, commonly returning this data as ActiveRecord collections. Finders interact with queries, and as such, returning an Active Record collection is an implicit practice but it's not documented anywhere.
Returning any other object, such as arrays, hashes, or single objects, has the following disadvantages:
- It's is surprising, e.g imagine
ClustersFinder.execute
returning a hash ofClusters
rather than aCluster
relationship. - Data returned by finder can't be easily concatenated to a model scope or any other query.
- Could lead to unexpected bugs (as one could assume the finder returns an ActiveRecord relationship). This is actually hard to spot during reviews.
Proposal
Ensure GitLab finders always return ActiveRecord collections by
- Documenting this in our database best practices
- Create a rubocop to catch these offenses early on the development.