How to find records by month Datamapper

2019-01-29 07:51发布

问题:

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?

回答1:

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