两个日期参数之间的ruby查询 [英] ruby query between two date parameters

查看:42
本文介绍了两个日期参数之间的ruby查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Result 的模型.我需要创建一个变量来返回某个日期范围内的所有结果.在 Result 模型上有一个名为 date 的字段,在一个表单上,我将开始和结束日期作为参数捕获并将它们传递回控制器.

I have a model called Result. I need to create a variable that returns all results that come in between a certain date range. On the Result model there is a field called date, and on a form I am capturing a start and end date as parameters and passing them back to the controller.

因此,如果用户在 startdate 参数中输入01/01/2014"并在参数中输入01/01/2015",我需要返回日期在此范围之间的所有结果.

so if the user enters '01/01/2014' in the startdate parameter and '01/01/2015 in the parameters I need to return all results where the date is between this range.

当用户按下过滤器"按钮时,参数最终被捕获为变量 startdate 和 enddate

When the user pressers a "filter" button the parameters end up being captured as variables startdate and enddate

我试过了,但它似乎不起作用

I tried this but it doesn't seem to work

@results = Result.where("date >= ? and date <= ?", startdate, enddate")

然后我查看了生成的 SQL 并认为它需要是这个

I then looked at the resulting SQL and thought it needed to be this

@results = Result.where("date >= ? and date <= ?", '#{startdate)', '#{enddate}')

有什么想法吗?

谢谢

推荐答案

感谢大家的建议.这就是我最终的解决方案.

Thanks to everyone for the advice. This was my solution in the end.

首先,使用 datepicker 确保只能在表单中输入有效日期,如下所示...

Firstly, ensure only valid dates can be entered in the form by using datepicker, which looks like this...

  $(function(){
    $('#startdate').datepicker({
      changeYear: true,
      dateFormat:"dd/mm/yy",
      defaultDate: "#{Date.today.day}/#{Date.today.month}/#{Date.today.year}",
      change: function() {
        var isoDate = this.getValue('yyyy-mm-dd');
        $(this).attr('data-value', isoDate);
      }
    });
$('#enddate').datepicker({
  changeYear: true,
  dateFormat:"dd/mm/yy",
  defaultDate: "#{Date.today.day}/#{Date.today.month}/#{Date.today.year}",
  change: function() {
    var isoDate = this.getValue('yyyy-mm-dd');
    $(this).attr('data-value', isoDate);
    }
  });
});


%form{:action => employee_results_path(@employee), :class => 'navbar-search pull-left'} 
  .field
    %input{:type =>'text', :placeholder => params[:startdate] != nil ? params[:startdate] : "from date", :id => 'startdate', :name => "startdate", :style => 'width: 100px'}
    %input{:type =>'text', :placeholder => params[:startdate] != nil ? params[:enddate] : "to date", :id => 'enddate', :name => "enddate", :style => 'width: 100px'}

然后,在控制器中将参数捕获为变量并将它们解析为日期

then, in the controller capture the parameters as variables and Parse them as dates

sdate = Date.parse(params[:startdate])
edate = Date.parse(params[:enddate])

然后使用我在这个问题中收到的有用的解决方案

Then use the helpful solution I recieved in this question

@results = Result.where(:date => sdate..edate)

这是一种享受.感谢所有帮助我学习的人!

which worked a treat. Thanks to everyone to helped me learn!

这篇关于两个日期参数之间的ruby查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆