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

推荐订阅源

宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
Jina AI
Jina AI
博客园 - 叶小钗
雷峰网
雷峰网
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
量子位

Codebase Audits & Rescue | Ally Piechowski

Most of your tech debt is free Your Setup Script Should Support Git Worktrees 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 Recor...
"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