Active Record - 查找今天之前 created_at 的记录 [英] Active Record - Find records which were created_at before today

查看:23
本文介绍了Active Record - 查找今天之前 created_at 的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取 created_at 字段小于今天(日期)的所有记录.有没有类似的东西:

I want to get all records where the created_at field is less than today (a date). Is there anything like:

MyTable.find_by_created_at(< 2.days.ago)

推荐答案

使用 ActiveRecord 标准方式:

MyModel.where("created_at < ?", 2.days.ago)

使用底层Arel接口:

MyModel.where(MyModel.arel_table[:created_at].lt(2.days.ago))

在 Arel 上使用一些薄层:

Using some thin layer over Arel:

MyModel.where(MyModel[:created_at] < 2.days.ago)

使用squeel:

MyModel.where { created_at < 2.days.ago }

这篇关于Active Record - 查找今天之前 created_at 的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆