如何按月查找记录Datamapper [英] How to find records by month Datamapper
本文介绍了如何按月查找记录Datamapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
添加报表用户可以选择放置报表的日期。 (not created_at)
我将此数据保存在Date中:DataTime格式列(2012-03-24T00:00:00 + 00:00)
用户从datepicker中选择日期,而不是解析与
DateTime.parse(2012-03-24)
而不保存到DB。 比用户通过datepicker选择一个月。
如何正确执行查询查找perticular月份的所有记录?
有没有办法按月查找?
解决方案
否。您应该计算保证金日期,并使用:date =>范围
(它将创建sql BETWEEN查询)。例如:
month = params [:month] .to_i
date_a = DateTime.new Date.today.year ,月,1
date_b =(date_a>> 1)+ 1#将添加一个月和一天
Page.all:updated_at => date_a..date_b
In my project I need to have ability ti add reports.
Adding a report user can choose date for placing his report. (not created_at)
I save this data in "Date":DataTime - format column(2012-03-24T00:00:00+00:00)
User selects date from datepicker and than i parse it with DateTime.parse(2012-03-24)
and than save to DB.
Than user via datepicker selects a month.
How can I correctly perform a query to find all records for perticular month?
Is there any method to find by month?
解决方案
No. You should calculate margin dates and use :date => range
(it will create sql BETWEEN query). For example:
month = params[:month].to_i
date_a = DateTime.new Date.today.year, month, 1
date_b = (date_a >> 1) + 1 # will add a month and a day
Page.all :updated_at => date_a..date_b
这篇关于如何按月查找记录Datamapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文