百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

squirrel

> 编程语言
开源

具有自然外观的 ActiveRecord 查询

178 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

具有自然外观的 ActiveRecord 查询

= DEPRECATED

Thoughtbot is no longer supporting this project, it hasn't been tested or used since at least Rails 2.x.

We recommend exploring Searchlogic (https://github.com/binarylogic/searchlogic) and Arel (https://github.com/rails/arel) for dynamic scopes.

= Squirrel

SQL Simplification Plugin for ActiveRecord

This plugin extends the ActiveRecord::Base#find method to be able to take a block of Ruby which will get parsed into a nice SQL string and have its results returned.

Author:: Jon Yurek (mailto:[email protected]) Copyright:: Copyright(c) 2008 thoughtbot, inc. License:: Distributes under the same terms as Ruby Website:: http://www.thoughtbot.com/projects/squirrel

Squirrel is a plugin for ActiveRecord which attempts to make SQL querier a much more natural prospect. You can write your queries in Ruby code and they get translated, including all proper table joins, into relevant SQL code and executed, returning your results.

User.find(:all) do first_name.contains? params[:first_name] posts.created_at >= 1.week.ago end

This query will return all Users that have a first_name that contains whatever was passed in as the "first_name" parameter, and that has any Posts that were created"in the past week. Both columns and associations are referenced as methods. Columns are referenced exactly as they are in the database, and associations are referenced exactly as they are specified in their respective has_many, belongs_to, etc calls. For example, in the snippet above, the User has_many :posts, and so we use "posts" as the method to refer to that association.

This mechanism works for all of ActiveRecord's associations, because it piggybacks on AR's eager loading functionality, which always produces the joins necessary for getting the columns required.

By default, all conditions specified in the query are ANDed together. If it is necessary to have any condition match, you can group your conditions together using the "any" method, which takes a block containing the conditions. For example:

Playlist.find(:all) do any do name == "Party Mix" total_length > 3600 end end

... will find all Playlists that either have a name of "Party Mix" or that have a total length of 1 hour (3600 seconds). There is also an "all" method that works similarly, but joins with "AND". These groups are nestable."

Currenly, there is no allowance in Squirrel for either grouping or fetching columns that aren't part of any of the included tables.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言