惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
S
Secure Thoughts
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tor Project blog
T
Tenable Blog
Know Your Adversary
Know Your Adversary
Webroot Blog
Webroot Blog
V
Vulnerabilities – Threatpost
WordPress大学
WordPress大学
S
Security @ Cisco Blogs
J
Java Code Geeks
S
SegmentFault 最新的问题
A
Arctic Wolf
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
Forbes - Security
Forbes - Security
爱范儿
爱范儿
T
The Blog of Author Tim Ferriss
S
Securelist
MongoDB | Blog
MongoDB | Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
月光博客
月光博客
Blog — PlanetScale
Blog — PlanetScale
博客园 - 【当耐特】
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
SecWiki News
SecWiki News
aimingoo的专栏
aimingoo的专栏
腾讯CDC
U
Unit 42
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
Latest news
Latest news
I
InfoQ
V2EX - 技术
V2EX - 技术
The Cloudflare Blog
V
V2EX
The Register - Security
The Register - Security
博客园 - Franky
Security Archives - TechRepublic
Security Archives - TechRepublic
Security Latest
Security Latest
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
小众软件
小众软件
N
Netflix TechBlog - Medium
量子位
M
MIT News - Artificial intelligence

Codebase Audits & Rescue | Ally Piechowski

How to Be a Good Open Source Maintainer "The Git Commands I Run Before Reading Any Code" "Ruby 3.2 Is EOL: What You Actually Need to Do" "Rails 7.2 to 8.1 Upgrade: What Actually Breaks and How to Fix It" "Why Your Engineering Team Is Slow (It's the Codebase, Not the People)" "Migrating from Sprockets to Propshaft: Is It Worth It?" "How to Close a Tab in Vim" "How I Audit a Legacy Rails Codebase in the First Week" "How to Open a New Tab in Vim" "Rails default_scope: Why You Should Never Use It" "Solved: ActionController::ParameterMissing (param is missing or the value is empty)" "Solved: Warning: Using the last argument as keyword parameters is deprecated" "Vim: How to Open Current Opened File in New Tab" "What is the best time for stand-up meetings?" "Using Let and Context to Modularize RSpec Tests" "What Is Fed vs Unfed Sourdough Starter?" "My Father, My Mentor, My Teacher" "How to Get Over Burnout" "What's the difference between a Good Developer and a Good Googler?" Codebase Audits & Rescue | Ally Piechowski
"Rails: How to Use Greater Than/Less Than in Active Record where Statements"
"Ally Piechowski" · 2019-12-31 · via Codebase Audits & Rescue | Ally Piechowski

When querying ActiveRecord for greater than and less than, most people turn to including raw SQL in their codebase... But raw SQL isn't required!

Ally Piechowski · · 2 min read
Rails: How to Use Greater Than/Less Than in Active Record where Statements

For example, if I want to find all users created within the last week, I would ask for all users from my User model in which the column updated_at is greater than 1 week ago, or 1.week.ago.

1. Using Ruby’s Infinity Range (Best)

In Rails 5+ and Ruby 2.6+, the infinity range can be used like so:

User.where(created_at: 1.week.ago..)

If you haven’t upgraded to Ruby 2.6 yet, you can specify the end of the range as the constant Float::INFINITY:

User.where(created_at: 1.week.ago..Float::INFINITY)

This is the best option because it is the closest syntax to actuality.

From 1 week ago until infinity == greater than or equal to 1 week ago

It also doesn’t have any of the caveats that using a raw string has (explained later in this post)

2. Using Active Record’s Arel Table

Utilizing arel directly, we can signal to ActiveRecord the logical SQL we want to do functionally:

User.where(User.arel_table[:created_at].gt(1.week.ago))

When faced with a challenging ActiveRecord Query that doesn’t fit into the core ruby-style syntax, I typically turn to Arel to provide me with a functional core and extra safety.

3. Using a Raw String (Worst)

In ActiveRecord, it’s possible to utilize a raw string to access sql’s interface directly, like so:

User.where('users.created_at > ?', 1.week.ago)

Or even the keyword interpolation style:

User.where('users.created_at > :after_date', after_date: 1.week.ago)

While this works, using SQL strings with ActiveRecord isn’t always the best due to:

  • Higher risk of accidental sql injection vulnerabilities
  • Reduced database adapter compatibility

Related Articles