我现在正在构建一个小型的Sinatra Web应用程序,并且在使用DataMapper理解查询时遇到了问题。
我的数据库中有一个字段,我称之为日期,我很乐意查询此字段,只返回当月的记录。
我试过这个:
query =" 2012-11"
@trips = Trip.all(:date.like =>'%query%')
这似乎不起作用,我希望有人可能会发现我的错误并给我一些指导!希望有人可以提供帮助,感谢您花时间阅读。
戴夫
答案 0 :(得分:0)
提供范围:
@trips = Trip.all(:date => (query..query))
我还会修复您的日期格式并提供月,日和年。
答案 1 :(得分:0)
require 'date'
date_c = Date.today
next_y, next_m = date_c.month == 12 ?
[date_c.year+1, 1] : [date_c.year, date_c.month+1]
date_a = Date.new(date_c.year, date_c.month)
date_z = Date.new(next_y, next_m)
Trip.all :date => date_a..date_z
这将返回当前月份条目