rails-query-object

Encapsulate complex Rails database queries in reusable query objects.

3|Updated Oct 16, 2025
One-click install
npx skills add https://github.com/dchuk/source_monitor --skill rails-query-object-dchuk
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rails-query-object
Source: https://github.com/dchuk/source_monitor/tree/main/.claude/skills/rails-query-object
Command: npx skills add https://github.com/dchuk/source_monitor --skill rails-query-object-dchuk

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Creates and tests query objects to encapsulate complex database queries, enabling clean, reusable data access patterns and testable business logic.

Core Features & Use Cases

  • Encapsulates multi-table queries with a dedicated query object
  • Supports context via constructor (user:, account:)
  • Returns ActiveRecord::Relation for chainability or Hash for aggregations
  • Includes TDD workflow and best practices for multi-tenant isolation

Quick Start

Create a query object for a given account and call its call method to retrieve the matching ActiveRecord relation.

Frequently Asked Questions about rails-query-object

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I encapsulate complex Rails queries for multi-tenant isolation?

Query objects encapsulate complex Rails queries by accepting a user or account context via the constructor, returning an ActiveRecord::Relation or Hash for clean multi-tenant isolation and reusable data access.

What is a query object in Rails and when should I use one?

A query object in Rails is a dedicated pattern that encapsulates complex multi-table database queries. You should use it for aggregation, report-like scenarios, and multi-tenant isolation to keep business logic testable and clean.

How do I test complex ActiveRecord queries with a TDD workflow?

You can test complex ActiveRecord queries using a test-driven workflow with query objects, which isolate multi-table logic and aggregations into reusable classes that accept context like user or account directly through their constructors.

Can I chain ActiveRecord::Relation results from a query object in Rails?

Yes, query objects in Rails can return an ActiveRecord::Relation, allowing you to chain additional methods directly. For aggregation and report-like scenarios, they can also return a Hash instead of a relation.

What's the best way to organize reusable data access patterns in Rails?

The best way to organize reusable data access patterns in Rails is using query objects with clear naming conventions. They enforce constructor-based context and return ActiveRecord::Relation or Hash, keeping complex queries maintainable.

Are there limitations to using query objects for Rails aggregations?

Query objects handle aggregations by returning a Hash instead of an ActiveRecord::Relation. This means you cannot chain ActiveRecord methods on Hash results, so you must design your query objects to finalize all needed aggregations internally.