使用活动记录来查找记录通过月份和日期,忽略一年时间 [英] Use active record to find a record by month and day, ignoring year and time
本文介绍了使用活动记录来查找记录通过月份和日期,忽略一年时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个模型(项),用五年的时间价值的纪录(每天一个记录)。我需要的是,在过去的日期对象,如的方法2011-12-25 00:00:00
,会显示出我的一切都发生在12/25的记录(查询对:created_at
列),这就是过去的一年或时间,无论
I have a model (Entries) with five years worth of records (one record per day). I need a method that, when passed a date object such as 2011-12-25 00:00:00
, will show me ALL the records that have happened on 12/25 (querying against the :created_at
column), regardless of the year or time that's passed.
回报率3.0.9 /红宝石1.9.2p290
RoR 3.0.9 / Ruby 1.9.2p290
推荐答案
您可以使用MySQL的月份和日期值。也许是这样的:
You can use the MONTH and DAY values of mysql. Maybe something like:
Model.where("MONTH(created_at) = ? and DAY(created_at) = ?", somedate.month, somedate.day)
这篇关于使用活动记录来查找记录通过月份和日期,忽略一年时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文