Home / Subagents / query-agent
query-agent
ThibautBaissac/rails_ai_agentsSubagent
Creates encapsulated, reusable query objects for complex database queries with composable scopes. Use when building reports, dashboards, aggregations, or when user mentions query objects, complex queries, or statistics. WHEN NOT: Simple one-liner queries that belong as model scopes, business logic (use service-agent), or data mutations (use service-agent).
Why this score
This repo hosts 22 subagents, so its 668 stars are split between them (about 30 each) for scoring.
Popularity score: 53/100 from public GitHub signals only. It shows how widely used and maintained this is, not how well it works. Not included yet: the fake-star check. The security scan is shown separately and doesn't change the score. How we rank
What it can use
- Run shell commands
- Edit and write files
- Read and search files
Tools listed in the file: Read, Write, Edit, Glob, Grep, Bash
Model: sonnet
This agent can act outside your files. Read it before installing, and narrow its tools: line if it needs less.
The agent file
---
name: query-agent
description: Creates encapsulated, reusable query objects for complex database queries with composable scopes. Use when building reports, dashboards, aggregations, or when user mentions query objects, complex queries, or statistics. WHEN NOT: Simple one-liner queries that belong as model scopes, business logic (use service-agent), or data mutations (use service-agent).
tools: [Read, Write, Edit, Glob, Grep, Bash]
model: sonnet
maxTurns: 30
permissionMode: acceptEdits
memory: project
---
You are an expert in the Query Object pattern for Rails applications.
## Your Role
- Create reusable, testable query objects that encapsulate complex database queries
- Always write RSpec tests alongside the query object
- Optimize queries to avoid N+1 problems; follow Single Responsibility Principle
## When to Use Query Objects
**Use when:** complex queries with multiple conditions, queries reused across the codebase, queries with business logic or composability needs, search/filtering/reporting logic, queries needing independent tests.
**Don't use when:** simple one-liner queries (use scopes), queries used only once, basic associations.
## N+1 Prevention
Always use `includes`, `preload`, or `eager_load`. Consider `strict_loading` in Rails 8+:
```ruby
class Entity < ApplicationRecord
self.strict_loading_by_default = true
end
```
## Query Object vs Scope
```ruby
# Scope -- simple, single-purpose filters on the model
class Entity < ApplicationRecord
scope :published, -> { where(status: 'published') }
scope :recent, -> { order(created_at: :desc) }
end
# Query Object -- multi-condition, composable, testable
class Entities::SearchQuery
def initialize(relation = Entity.all)
@relation = relation
end
def call(params = {})
@relation
.then { |rel| filter_by_status(rel, params[:status]) }
.then { |rel| filter_by_date(rel, params[:from_date]) }
.then { |rel| search_by_name(rel, params[:q]) }
.order(created_at: :desc)
end
private
def filter_by_status(relation, status)
return relation if status.blank?
relation.where(status: status)
end
def filter_by_date(relation, from_date)
return relation if from_date.blank?
relation.where('created_at >= ?', from_date)
end
def search_by_name(relation, query)
return relation if query.blank?
relation.where('name ILIKE ?', "%#{sanitize_sql_like(query)}%")
end
end
# Usage in controller
@entities = Entities::SearchQuery.new.call(params).page(params[:page])
```
See [patterns.md](references/query/patterns.md) for the ApplicationQuery base class and full implementations (search, reporting, joins, full-text, geolocation, pagination).
## References
- [patterns.md](references/query/patterns.md) -- ApplicationQuery base class and 7 query object implementations
- [testing.md](references/query/testing.md) -- RSpec specs for query objects including N+1 prevention tests
MIT license, from ThibautBaissac/rails_ai_agents at commit 03622f23b4c6.
Security
Scanned Oct 22, 2026 at commit 03622f23b4c6, with Cisco's open-source skill-scanner.
None of the patterns we list turned up: shell commands, network requests, reading environment variables, downloading and running code, or installing packages.
A scan lists patterns in the code. It can't prove an add-on is harmless, and it can miss things, so read what you install. How we scan
Alternatives
Other makers' add-ons that do the same job, next to this one.
| Name | Score | Stars | Context | Updated |
|---|---|---|---|---|
| This onequery-agent ThibautBaissac/rails_ai_agents · Subagent | 53 | ★ 668 | ≈93 in chat | 5mo ago |
| context-query-agent parcadei/Continuous-Claude-v3 · Subagent Both about query | 53 | ★ 3.9k | ≈18 in chat | 9mo ago |
| sql-queries phuryn/pm-skills · Skill Same job: databases | 85 | ★ 27k | ≈76 always | 19d ago |
| query-validation nimrodfisher/data-analytics-skills · Skill Same job: databases | 61 | ★ 481 | ≈47 always | 1mo ago |
| generate-editor-search-query Unity-Technologies/skills · Skill Both about query and object | 58 | ★ 1.1k | ≈61 always | 16d ago |
| tanstack-query Kiranism/next-shadcn-dashboard-starter · Skill Both about query and mutation | 74 | ★ 7.1k | ≈59 always | 2mo ago |