rails初心者ですが勉強のためお金管理のシステムを作っています。
whereとeachを使っての計算結果の合計の出し方について困っています。
定期口座にお金を入れると2か月後、3か月後から毎月利息が入る設定で、定期口座から出金はありませんが途中で入金する場合もあります。その場合20日を締め日として口座着金日からの2か月後、3か月後の月末に利息が入るようにしています。
入金の日から現在の利息分はそれぞれ出せたのですが、その利息を合計したものの出し方がわからず困っています。
いろいろ検索したのですがよいメソッドなども見つからなく、アドバイスいただけたら幸いです。
お願いします。
該当のソースコード
controller
1def index 2 @client = Client.find_by(id: params[:id]) 3 @cashes = Cash.where(user_id: @client.id ).order(trading_day: :desc) 4 5 @principal_total = @cashes.sum { |hash| hash[:principal]} 6 #定期口座の合計 7 8 @now_20day = Date.today.beginning_of_month + 20 9 #今月20日
view
1<% @cashes.each do |cash| %> 2 <%= cash.user_id %> 3 <%= cash.receipt_of_payment %> 4 <%= cash.principal.to_s(:delimited) %> 5 <% if cash.receipt_of_payment != nil 6 if cash.receipt_of_payment.day < 21 7 @receipot_of_payment_2month_since = cash.receipt_of_payment.since(2.month) 8 #着金2か月後 9 @payment_2month_end = @receipot_of_payment_2month_since.end_of_month 10 #着金2か月後の月末 11 @interest_month = (@now_20day.year * 12 + @now_20day.month) - 12 (@payment_2month_end.year * 12 + @payment_2month_end.month) 13 #利息月数 14 @interest = @interest_month * 0.01 * cash.principal 15 #利息額 16 else cash.receipt_of_payment.day > 20 17 @receipot_of_payment_3month_since = cash.receipt_of_payment.since(3.month) 18 #着金3か月後 19 @payment_3month_end = @receipot_of_payment_3month_since.end_of_month 20 #着金3か月後の月末 21 @interest_month = (@now_20day.year * 12 + @now_20day.month) - 22 (@payment_3month_end.year * 12 + @payment_3month_end.month) 23 #利息月数 24 @interest = @interest_month * 0.01 * cash.principal 25 #利息額 26 end 27 end 28 %> 29 <%= @interest.floor.to_s(:delimited) %> 30 <%= link_to("編集","/cash/#{cash.id}/edit") %> 31 <%= link_to("削除","/cash/#{cash.id}/destroy", 32 {method:"post"}) %> 33<% end %>
付いて行くのがかなりしんどいご質問です。どの変数が何で、どれとどれを足し合わせたらどうなってとか、つまるところこれとこれを足したいけどうまくいかないとか、親身な説明がないと、誰も読むことすらせず理解もできません。結果として答えはえられないと思います。
あなたの回答
tips
プレビュー