我正在为我的 Rails 应用程序实现一个全文搜索 API,到目前为止,Thinking Sphinx 取得了巨大的成功。
我现在想实现日期范围搜索,并不断收到“范围错误”错误。
这是控制器代码的片段,我有点不知道下一步该做什么。
@search_options = { :page => params[:page], :per_page => params[:per_page]||50 }
unless params[:since].blank?
# make sure date is in specified format - YYYY-MM-DD
d = nil
begin
d = DateTime.strptime(params[:since], '%Y-%m-%d')
rescue
raise ArgumentError, "Value for since parameter is not a valid date - please use format YYYY-MM-DD"
end
@search_options.merge!(:with => {:post_date => d..Time.now.utc})
end
logger.info @search_options
@posts = Post.search(params[:q], @search_options)
当我查看日志时,我看到这一位似乎暗示日期尚未转换为与 Time.now.utc 相同的时间格式。
withpost_date2010-05-25T00:00:00+00:00..Tue Jun 01 17:45:13 UTC 2010
有任何想法吗?基本上,我试图让 API 请求在“自”日期传递,以查看某个日期之后的所有帖子。我指定日期应为 YYYY-MM-DD 格式。
谢谢你的帮助。克里斯
编辑: 我刚刚将日期参数合并语句更改为此
@search_options.merge!(:with => {:post_date => d.to_date..DateTime.now})
现在我得到这个错误
2010 年 5 月 25 日星期二的未定义方法“to_i”:日期
所以显然有些东西仍然没有设置正确......