Ruby query between two date parameters

I have a model called Result. I need to create a variable that returns all results between a specific date range. The result model has a field called a date, and in the form I take the start and end date as parameters and pass them back to the controller.

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

When the user clicks the filter button, the parameters are eventually captured as startdate and enddate variables

I tried this but it does not work

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

Then I looked at the resulting SQL and thought it should be

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

Any ideas?

thank

+2
3

. .

-, , , datepicker, :

  $(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'}

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

,

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

. , !

+1

@MrYoshiji, , startdate enddate Date, DateTime Time. .

1.) datepicker input. datepicker.

2.) .

3.) regex, .

, :

@results = Result.where(date: Date.parse(startdate).beginning_of_day..Date.parse(enddate).end_of_day)

, , 500 .

rescue Result.all ( ALL-ELSE-FAILED)

, , . postgresql - UTC.

+1

@results = Result.where(:date => startdate..enddate)

startdate enddate Date.

0

All Articles