rails-query-object

Encapsulate complex Rails database queries into reusable query objects.

643|80|Updated Dec 9, 2025
One-click install
npx skills add https://github.com/ThibautBaissac/rails_ai_agents --skill rails-query-object
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rails-query-object
Source: https://github.com/ThibautBaissac/rails_ai_agents/tree/main/skills/rails-query-object
Command: npx skills add https://github.com/ThibautBaissac/rails_ai_agents --skill rails-query-object

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Encapsulates complex queries into reusable query objects to simplify testing, readability, and maintenance in Rails apps using a test-driven development approach.

Core Features & Use Cases

  • Encapsulates complex queries into dedicated query objects to improve testability and reusability.
  • Supports multi-tenant contexts by scoping queries to accounts or users.
  • Provides a clear workflow for building, testing, and integrating query objects in controllers or services.

Quick Start

Write a failing spec in spec/queries for your new query object, then implement the class in app/queries, and run the tests to verify GREEN.

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 ActiveRecord queries into reusable objects?

Encapsulate complex Rails ActiveRecord queries into reusable query objects by defining dedicated classes in app/queries that accept a context and return relations or hashes. This isolates query logic for better testability and maintenance.

How do I test complex database queries in Rails using a test-driven approach?

Test complex database queries in Rails by writing a failing spec in spec/queries first, then implementing the query object class in app/queries. Run the tests to verify GREEN before integrating the logic into controllers or services.

What is the best way to handle multi-tenant scoped queries in Rails?

Handle multi-tenant scoped queries in Rails by using query objects that accept an account or user context in their constructor. This ensures all aggregated statistics and dashboard data are automatically scoped to the correct tenant.

When should I use query objects instead of standard ActiveRecord scopes in Rails?

Use query objects instead of standard ActiveRecord scopes when you need to encapsulate complex database queries for dashboards, reports, and aggregated statistics. They provide isolated, testable logic that prevents fat models or controllers.

Can I use query objects to return hashes instead of ActiveRecord relations for Rails reports?

Yes, query objects can return hashes instead of ActiveRecord relations for Rails reports. The constructor accepts a context like an account or user, allowing flexible data structures for aggregated statistics and dashboard rendering.