0

I'm building a little Sinatra web app at the moment and having problems understanding queries with DataMapper.

I have a field in my database which I've called date, I'd love to query this and only return records from this current month.

I've tried this:

query = "2012-11"

@trips = Trip.all(:date.like => '%query%')

Which doesn't seem to work, I'm hoping someone might spot my error and give me some guidance! Hope someone can help, thanks for taking the time to read.

Dave

4

2 回答 2

0

改为提供一个范围:

@trips = Trip.all(:date => (query..query))

我还将修复您的日期格式并提供月、日和年。

于 2012-11-29T21:35:44.103 回答
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

这将返回当前月份条目

于 2012-11-29T21:47:13.497 回答