我有一个表,其中包含大量数据(交易数据),我想对其进行全面报告。该表具有Currency
,Fee charged
,Network Fee Charged
,Order Type
,Created_at
,Updated_at
。例如,在此报告中,我希望根据所选的日期时间范围来计算fees
的总和,如果未选择日期时间,则默认为30天,并根据数据库表中的所有唯一货币计算费用总和。
在实现这一目标时,这是我所做的,它不起作用,但是遇到了undefined method `[]' for nil:NilClass Error
。
module Admin
module Statistic
class RevenuesController < BaseController
def show
@revenues_grid = ::Statistic::RevenuesGrid.new(params[:statistic_revenues_grid])
@revenues_reports = []
Currency.all.each do |currency|
start_datetime = params.nil? ? 30.days.ago : params[:created_at][0] # This throws the error
end_datetime = params.nil? ? Time.now : params[:created_at][1] # This throws the error too
revenues_report_for_currency = get_revenue_report_for_currency(currency, start_datetime, end_datetime)
@revenues_reports << revenues_report_for_currency
end
end
def get_revenue_report_for_currency(currency, start_datetime, end_datetime)
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
{
: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
undefined method `[]' for nil:NilClass Error
?答案 0 :(得分:2)
在params.nil?
行中更改start_datetime = params.nil? ? 30.days.ago : params[:created_at][0]
成为params[:created_at].nil?
更改后您的代码将为:
module Admin
module Statistic
class RevenuesController < BaseController
def show
@revenues_grid = ::Statistic::RevenuesGrid.new(params[:statistic_revenues_grid])
@revenues_reports = []
Currency.all.each do |currency|
start_datetime = params[:created_at].nil? ? 30.days.ago : params[:created_at][0] # This shouldn't throw error
end_datetime = params[:created_at].nil? ? Time.now : params[:created_at][1] # This shouldn't throw error
revenues_report_for_currency = get_revenue_report_for_currency(currency, start_datetime, end_datetime)
@revenues_reports << revenues_report_for_currency
end
end
def get_revenue_report_for_currency(currency, start_datetime, end_datetime)
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
{
: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
更好的方法是替换
start_datetime = params[:created_at].nil? ? 30.days.ago : params[:created_at][0]
end_datetime = params[:created_at].nil? ? Time.now : params[:created_at][1]
使用
start_datetime, end_datetime = params[:created_at].nil? ? [30.days.ago, Time.now] : params[:created_at]