按 created_at(start_datetime 和 end_datetime)过滤只会选择一个日期 end_datetime

Filtering by created_at (start_datetime and end_datetime) picks only one date which is end_datetime

我正在尝试为我的 revenue_controller 表演动作 创建一个过滤器,但效果不佳。

这是我在下面的代码中所做的,但是当我按日期过滤时它会单独选择 end_datetime

revenue_controller.rb

我在 show 操作中尝试做的是默认我的搜索为从现在起 30 天,如果参数填充了日期,则使用选定的日期时间。

module Admin
  module Statistic
    class RevenuesController < BaseController

      def show
        @revenues_reports = Currency.all.map do |currency|
          puts params
          start_datetime = if params[:created_at].nil? then
                             30.days.ago
                           else
                             params[:created_at].slice(0..15)
                           end
          end_datetime = if params[:created_at].nil? then
                           Time.now
                         else
                           params[:created_at].slice(0..15)
                         end
          get_revenue_report_for_currency(currency, start_datetime, end_datetime)
        end
      end

      def get_revenue_report_for_currency(currency, start_datetime, end_datetime)
        puts "start: #{start_datetime}" # I added this to see from the log the start_datetime it picks
        puts "end: #{end_datetime}" # I added this to see from the log the end_datetime it picks
        total_withdraw_fees_charged = Withdraw.where(:currency => currency.id, :created_at => start_datetime..end_datetime).sum(:fee)
        total_withdraw_network_fees_charged = 0.3 * total_withdraw_fees_charged
        net_income_from_withdraws = total_withdraw_fees_charged - total_withdraw_network_fees_charged

        total_sell_order_fees_charged = Order.where(:type => 'OrderAsk', :bid => currency.id, :created_at => start_datetime..end_datetime).sum(:fee)
        total_buy_order_fees_charged = Order.where(:type => 'OrderBid', :ask => currency.id, :created_at => start_datetime..end_datetime).sum(:fee)
        total_trading_fees = total_sell_order_fees_charged + total_buy_order_fees_charged

        total_fees = net_income_from_withdraws + total_trading_fees

        {
          currency: currency.code.upcase,
          total_withdraw_fees_charged: total_withdraw_fees_charged,
          total_withdraw_network_fees_charged: total_withdraw_network_fees_charged,
          net_income_from_withdraws: net_income_from_withdraws,
          total_sell_order_fees_charged: total_sell_order_fees_charged,
          total_buy_order_fees_charged: total_buy_order_fees_charged,
          total_trading_fees: total_trading_fees,
          total_fees: total_fees
        }
      end
    end
  end
end

show.html.erb

.panel.panel-primary
  .panel-heading
    h4.panel-title = t('admin.statistic.filter')
  .panel-body
    = form_tag(admin_statistic_revenue_path, :method => 'get', class: 'form-horizontal') do |f|
      .row
        .col.col-xs-3
          label[for="#"]
            | Start Date:
            input.form-control.created_at.date_time_filter.from[name="[created_at]" type="#" value=""]
        .col.col-xs-3
          label[for="#"]
            | End Date:
            input.form-control.created_at.date_time_filter.to[name="[created_at]" type="#" value=""]
        = button_tag "Search", :class => 'btn btn-info', :name => nil

浏览器参数url

http://localhost:3000/admin/statistic/revenue?utf8=%E2%9C%93&%5Bcreated_at%5D=2018-08-01+00%3A00&%5Bcreated_at%5D=2018-10-05+00%3A00

development_logs

您正在尝试对两个不同的参数 created_at_fromcreated_at_to 使用单一参数名称:

    .col.col-xs-3
      label[for="#"]
        | Start Date:
        input.form-control.created_at.date_time_filter.from[name="created_at_from" type="#" value=""]
    .col.col-xs-3
      label[for="#"]
        | End Date:
        input.form-control.created_at.date_time_filter.to[name="created_at_to" type="#" value=""]

created_at 通常是 datetime,因此大多数记录的时间都不是零。 并且 2018-10-05 12:34:56 +0100 大于 2018-10-05 00:00:00.

'records created at specific date' 的正确过滤器是 'records created after that day has started, but before and of day':

start_datetime = if params[:created_at_from].present?
                   Time.zone.parse(params[:created_at_from]).beginning_of_day
                 else
                   30.days.ago
                 end
end_datetime = if params[:created_at_to].present?
                 Time.zone.parse(params[:created_at_to]).end_of_day
               else
                 Time.zone.now
               end

另请注意 zone,因为根据时区的不同,一天可能会以不同的世界标准时间开始。