Skip to content

Latest commit

 

History

History
23 lines (17 loc) · 1.28 KB

query_objects.md

File metadata and controls

23 lines (17 loc) · 1.28 KB

Query Objects

Introduction

Query Objects are useful to extract ActiveRecord scopes or complex SQL queries into their own classes, which would otherwise clutter ActiveRecord models.

Conventions

  • Queries go under the app/queries directory.
  • Query class name should have suffix Query (e.g. AuthorsQuery).
  • Query class name should start with primary model name used in query in plural form (e.g. AuthorsWithBooksQuery).
  • Consider creating base query class for the model (e.g. AuthorsQuery) and subclasses for more specific cases (e.g. AuthorsWithBooksQuery < AuthorsQuery).
  • Query object should return ActiveRecord::Relation object.
  • Use scope none for the cases when respond with empty collection is required.
  • Define only one public method #perform.
  • Use auxiliary_rails gem.

References