消除循环会导致滑轨

时间:2018-10-09 09:36:47

标签: ruby-on-rails ruby ruby-on-rails-4 datagrid

我正在处理一种订单,其中我在一个属性中拥有多种不同货币的金额。因此,我尝试使用表中的其他属性在该属性内的不同货币上制作sum,这种方法可以正常工作,但是将结果作为所有行的计数输出,而不是仅显示所计算的随机值的总和。 / p>

orders_controller.rb

module Admin
  module Statistic
    class OrdersController < BaseController
      def show
        @orders_grid = ::Statistic::OrdersGrid.new(params[:statistic_orders_grid]) do |value|
          value.page(params[:page]).per(20)
        end
        @assets = @orders_grid.assets


        #@fee_groups = {:fee => @assets.sum(:fee)}
        @fee_groups = {
          :fee => @assets.sum{|t|
            olaoa = t.type
            market_string = t.currency
            base_currency = market_string.slice(0..2)
            quote_currency = market_string.slice(3..5)
            if olaoa == 'OrderBid' and base_currency == 'btc'
              "#{ t.fee.to_s + ' ' + base_currency.upcase }"
            elsif olaoa == 'OrderAsk' and quote_currency == 'ngn'
              "#{ t.fee.to_s + ' ' + quote_currency.upcase }"
            end
          }
        }

        @orders_filter = true
        @orders_group = true
      end
    end
  end
end

summary.htm.slim

.panel.panel-default
  .panel-heading
    h4.panel-title.row
      a data-parent="#filter-accordion" data-toggle="collapse" href="#summary"
        span.col-xs-8.text-muted = t('admin.statistic.summary')
        span.col-xs-4.text-right.text-muted = t('admin.statistic.click-to-expand')
  #summary.panel-collapse.collapse
    .panel-body
      .datagrid-groups
        - if !@orders_group
          - if groups
            - groups.each do |key, val|
              .datagrid.group.row
                span.col-xs-2.title = t("admin.statistic.#{controller_name}.#{controller.action_name}.#{key}")
                span.col-xs-10.value = val
        - if @orders_group
          / Summary Count Loop
          - if groups
            - groups.each do |key, val|
              .datagrid.group.row
                span.col-xs-2.title = t("admin.statistic.#{controller_name}.#{controller.action_name}.#{key}")
                span.col-xs-10.value = pluralize(val, 'Order')
          / Summary Fees Loop. This is the Fee loop causing problem if am rigth and I dont know how to fix this.
          - if @fee_groups
            - @fee_groups.each do |key, val|
              .datagrid.group.row
                span.col-xs-2.title = t("admin.statistic.#{controller_name}.#{controller.action_name}.#{key}")
                span.col-xs-10.value = val

代码结果

Result Image

所以您可以看到它渲染5次0.0BTC,因为过滤器只有5个订单。我该如何处理。我只想在结果中显示所有BTC的总和,而不是显示5次。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

因为在@fee_groups查询中对字符串求和,这将导致字符串彼此并排而不是合计。

如果您这样称呼

@fee_groups = { fee: @assets.sum{|t| t.fee}}

您将获得资产的总和。