Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
B

baby_squeel

> 数据库
Open source

:pig: An expressive query DSL for Active Record

507 stars0 likes2 views
WebsiteGitHub

About

:pig: An expressive query DSL for Active Record

BabySqueel

Have you ever used the Squeel gem? It's a really nice way to build complex queries. However, Squeel monkeypatches Active Record internals, because it was aimed at enhancing the existing API with the aim of inclusion into Rails. However, that inclusion never happened, and it left Squeel susceptible to breakage from arbitrary changes in Active Record, eventually burning out the maintainer.

BabySqueel provides a Squeel-like query DSL for Active Record while hopefully avoiding the majority of the version upgrade difficulties via a minimum of monkeypatching. :heart:

Installation

Add this line to your application's Gemfile:

ruby
gem 'baby_squeel'

And then execute:

$ bundle

Or install it yourself as:

$ gem install baby_squeel

Introduction

With Active Record, you might write something like this:

ruby
Post.where('created_at >= ?', 2.weeks.ago)

But then someone tells you, "Hey, you should use Arel!". So you convert your query to use Arel:

ruby
Post.where(Post.arel_table[:created_at].gteq(2.weeks.ago))

Well, that's great, but it's also pretty verbose. Why don't you give BabySqueel a try:

ruby
Post.where.has { created_at >= 2.weeks.ago }

Quick note

BabySqueel's blocks use instance_eval, which means you won't have access to your instance variables or methods. Don't worry, there's a really easy solution. Just give arity to the block:

ruby
Post.where.has { |post| post.created_at >= 2.weeks.ago }

Usage

Okay, so we have some models:

…
Orders
ruby
Post.ordering { [id.desc, title.asc] }
# SELECT "posts".* FROM "posts"
# ORDER BY "posts"."id" DESC, "posts"."title" ASC

Post.ordering { (id * 5).desc }
# SELECT "posts".* FROM "posts"
# ORDER BY "posts"."id" * 5 DESC

Post.select(:author_id).group(:author_id).ordering { id.count.desc }
# SELECT "posts"."author_id" FROM "posts"
# GROUP BY "posts"."author_id"
# ORDER BY COUNT("posts"."id") DESC

Post.joins(:author).ordering { author.id.desc }
# SELECT "posts".* FROM "posts"
# INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
# ORDER BY "authors"."id" DESC
Joins
…
Grouping
ruby
Post.selecting { id.count }.grouping { author_id }.when_having { id.count > 5 }
# SELECT COUNT("posts"."id") FROM "posts"
# GROUP BY "posts"."author_id"
# HAVING (COUNT("posts"."id") > 5)
Functions
ruby
Post.selecting { coalesce(author_id, 5).as('author_id_with_default') }
# SELECT coalesce("posts"."author_id", 5) AS author_id_with_default FROM "posts"
Subqueries
ruby
Post.joins(:author).where.has {
  author.id.in Author.select(:id).where(name: 'Ray')
}
# SELECT "posts".* FROM "posts"
# INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
# WHERE "authors"."id" IN (
#   SELECT "authors"."id" FROM "authors"
#   WHERE "authors"."name" = 'Ray'
# )
Exists
ruby
Post.where.has {
  exists Post.where.has { author_id == 1 }
}
# SELECT "posts".* FROM "posts"
# WHERE (
#   EXISTS(
#     SELECT "posts".* FROM "posts"
#     WHERE "posts"."author_id" = 1
#   )
# )
Custom SQL Operators
ruby
authors = Author.selecting { name.op('||', quoted('-dizzle')).as('swag') }
# SELECT "authors"."name" || '-dizzle' AS swag FROM "authors"

authors.first.swag #=> 'Ray Zane-dizzle'
Querying tables without Active Record models
ruby
table = BabySqueel[:some_table]

Post.joining {
  table.on(table.post_id == id)
}.where.has {
  table.some_column == 1
}
Polymorphism

Given this polymorphism:

ruby
# app/models/picture.rb
belongs_to :imageable, polymorphic: true

# app/models/post.rb
has_many :pictures, as: :imageable

The query might look like this:

ruby
Picture.
  joining { imageable.of(Post) }.
  selecting { imageable.of(Post).id }
Helpers
ruby
# SQL Literals
Post.select('1 as one').ordering { sql('one').desc }

# Quoting
Post.selecting { title.op('||', quoted('diddly')) }

# Functions
Post.selecting { func('coalesce', id, 1) }

Sifters

Sifters are like little snippets of conditions that can take arguments.

ruby
class Post  7.1.6'; rm Gemfile.lock; bin/setup; rake
export AR='~> 7.2.3'; rm Gemfile.lock; bin/setup; rake
export AR='~> 8.0.4'; rm Gemfile.lock; bin/setup; rake
export AR='~> 8.1.1'; rm Gemfile.lock; bin/setup; rake

You can also run bin/console to open up a prompt where you'll have access to some models to experiment with.

Rails update

  1. Update baby_squeel.gemspec
  2. Add the version to test matrix build.yml
  3. Update development section in the README.md
  4. If you need to change the code consider to add a version check methode in version_helper.rb
  5. Run the specs with all supported versions
  6. Add comment to the unreleased section in CHANGELOG.md

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rzane/baby_squeel.

License

The gem is available as open source under the terms of the MIT License.

Issues· 22 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyactiverecordareldslorm

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库