ruby
1 if notes_all.where('profit_and_loss > 0').present? && notes_all.where('profit_and_loss < 0').present? 2 @profit_factor=notes_all.where('profit_and_loss > 0').group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").sum(:profit_and_loss).values.map.with_index{|money,index| [notes_all.where('profit_and_loss > 0').group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").sum(:profit_and_loss).keys[index],money/notes_all.where('profit_and_loss < 0').group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").sum(:profit_and_loss).values[index].abs]}
この部分のコードに対して、
ruby
1undefined method `abs' for nil:NilClass
というエラーが出ます。
ifで条件分岐してnillclassになる場合は避けているはずなのですが、なぜエラーになるのでしょうか?
教えていただけると幸いです。
ご迷惑をおかけしますがよろしくお願い申し上げます。
以下に全体のコードを載せておきます。
ruby
1def index 2 @user=current_user 3 @notes=@user.notes.all.order("id DESC").page(params[:page]).per(30) 4 notes_all=@user.notes 5 if notes_all.present? 6 @profit_symbol=notes_all.group(:symbol).sum(:profit_and_loss) 7 profit_month=notes_all.group("YEAR(exit_datetime)").group("MONTH(exit_datetime)") 8 @profit_changes = profit_month.sum(:profit_and_loss).values.map.with_index{|money,index| [profit_month.sum(:profit_and_loss).keys[index],money/notes_all.sum(:profit_and_loss)*100]} 9 @profit_times=notes_all.group("HOUR(CONVERT_TZ(exit_datetime, '+00:00', '+09:00'))").sum(:profit_and_loss).values.map.with_index{|money, index| [notes_all.group("HOUR(CONVERT_TZ(exit_datetime, '+00:00', '+09:00'))").sum(:profit_and_loss).keys[index],money/notes_all.sum(:profit_and_loss)*100]} 10 @victry_times=notes_all.where('profit_and_loss > 0').group("HOUR(CONVERT_TZ(exit_datetime, '+00:00', '+09:00'))").count.values.map.with_index{|money,index| [notes_all.group("HOUR(CONVERT_TZ(exit_datetime, '+00:00', '+09:00'))").count.keys[index],money*100/notes_all.group("HOUR(CONVERT_TZ(exit_datetime, '+00:00', '+09:00'))").count.values[index]]} 11 @victory_rates=notes_all.where('profit_and_loss > 0').group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").count.values.map.with_index{|money,index| [notes_all.group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").count.keys[index],money*100/notes_all.group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").count.values[index]]} 12 if notes_all.where(:buy_sell==1).present? 13 @profit_short=notes_all.where(:buy_sell==1).group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").sum(:profit_and_loss).values.map.with_index{|money,index| [notes_all.group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").count.keys[index],money*100/notes_all.group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").sum(:profit_and_loss).values[index]]} 14 @victry_short=notes_all.where(:buy_sell==1).group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").count.values.map.with_index{|money,index| [notes_all.group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").count.keys[index],money*100/notes_all.group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").count.values[index]]} 15 end 16 if notes_all.where(:buy_sell==0).present? 17 @profit_long=notes_all.where(:buy_sell==0).group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").sum(:profit_and_loss).values.map.with_index{|money,index| [notes_all.group("MONTH(exit_datetime)").count.keys[index],money*100/notes_all.group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").sum(:profit_and_loss).values[index]]} 18 @victry_long=notes_all.where(:buy_sell==0).group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").count.values.map.with_index{|money,index| [notes_all.group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").count.keys[index],money*100/notes_all.group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").count.values[index]]} 19 end 20 this_month = Date.today.all_month 21 before_month=Date.today.last_month 22 profit_this_month = [] 23 profit_before_month = [] 24 notes_all.each do |note| 25 if this_month.include?(Date.parse(note[:created_at].to_s)) 26 profit_this_month << note.profit_and_loss 27 elsif before_month.include?(Date.parse(note[:created_at].to_s)) 28 profit_before_month << note.profit_and_loss 29 end 30 end 31 @profit_this_month= profit_this_month.sum 32 @profit_this_month_average = @profit_this_month / profit_this_month.length 33 @victory_rate_this_month = profit_this_month.select{|money| money > 0}.count/profit_this_month.count*100 34 if profit_before_month.present? 35 @profit_before_month= profit_before_month.sum 36 @profit_before_month_average= @profit_before_month / profit_before_month.length 37 @victory_rate_before_month=profit_before_month.select{|money| money > 0}.count/profit_before_month.count*100 38 end 39 if notes_all.where('profit_and_loss > 0').present? && notes_all.where('profit_and_loss < 0').present? 40 @profit_factor=notes_all.where('profit_and_loss > 0').group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").sum(:profit_and_loss).values.map.with_index{|money,index| [notes_all.where('profit_and_loss > 0').group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").sum(:profit_and_loss).keys[index],money/notes_all.where('profit_and_loss < 0').group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").sum(:profit_and_loss).values[index].abs]} 41 @risk_reward_factor=notes_all.where('profit_and_loss > 0').group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").average(:profit_and_loss).values.map.with_index{|money,index| [notes_all.where('profit_and_loss > 0').group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").average(:profit_and_loss).keys[index],money/notes_all.where('profit_and_loss < 0').group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").average(:profit_and_loss).values[index].abs]} 42 @recovery_factor=profit_month.sum(:profit_and_loss).values.map.with_index{|money,index| [profit_month.sum(:profit_and_loss).keys[index],money/notes_all.where('profit_and_loss < 0').group("YEAR(exit_datetime)").group("MONTH(exit_datetime)").sum(:profit_and_loss).values[index].abs]} 43 end 44 respond_to do |format| 45 format.html 46 format.csv 47 format.csv do |csv| 48 send_notes_csv(notes_all) 49 end 50 end 51 end 52 end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/20 07:27