質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

Q&A

1回答

1762閲覧

家計簿アプリ作成中です。各カラムの合計値を算出したい。

sh11821783

総合スコア0

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

0グッド

0クリップ

投稿2021/01/28 09:18

家計簿アプリ作成中です。各カラムの合計値を算出したい。

ruby(ruby on rails)で家計簿システムを作っています。
カレンダーのような表を出していまして、『電気代』や『ガス代』などの項目の下に各金額のカラムを出しているのですが、
各金額カラムの合計値を算出する機能を実装中なのですが、1日目のカラムの数値の合計しか拾うことができません。

【ユーザーコントローラ】 def show @household_account_book = @user.household_account_books.find_by(worked_on: @first_day..@last_day) end 【show画面のhtmlファイル】 <% provide(:title, @user.name) %> <div> <table class="table table-bordered table-condensed user-table"> <tr> <td>名前:<%= @user.name %></td> </tr> </table> </div> <div class="btn-users-show"> <%= link_to "⇦ 前月へ", user_path(date: @first_day.prev_month), class: "btn btn-info" %> <%= link_to "1ヶ月の家計簿編集へ", household_account_books_edit_one_month_user_path(date: @first_day), class: "btn btn-success" %> <%= link_to "次月へ ⇨", user_path(date: @first_day.next_month), class: "btn btn-info" %> </div> <div> <table class="table table-bordered table-condensed table-hover" id="table-attendances"> <thead> <tr> <th>日付</th> <th>曜日</th> <% if @household_account_book.fixed_cost.present? %> <th><%= @household_account_book.fixed_cost %></th> <% end %> <% if @household_account_book.fixed_cost_1.present? %> <th><%= @household_account_book.fixed_cost_1 %></th> <% end %> <% if @household_account_book.fixed_cost_2.present? %> <th><%= @household_account_book.fixed_cost_2 %></th> <% end %> <% if @household_account_book.fixed_cost_3.present? %> <th><%= @household_account_book.fixed_cost_3 %></th> <% end %> <% if @household_account_book.fixed_cost_4.present? %> <th><%= @household_account_book.fixed_cost_4 %></th> <% end %> <% if @household_account_book.fixed_cost_5.present? %> <th><%= @household_account_book.fixed_cost_5 %></th> <% end %> <% if @household_account_book.fixed_cost_6.present? %> <th><%= @household_account_book.fixed_cost_6 %></th> <% end %> <% if @household_account_book.fixed_cost_7.present? %> <th><%= @household_account_book.fixed_cost_7 %></th> <% end %> <% if @household_account_book.fixed_cost_8.present? %> <th><%= @household_account_book.fixed_cost_8 %></th> <% end %> <% if @household_account_book.fixed_cost_9.present? %> <th><%= @household_account_book.fixed_cost_9 %></th> <% end %> <% if @household_account_book.fixed_cost_10.present? %> <th><%= @household_account_book.fixed_cost_10 %></th> <% end %> <% if @household_account_book.fixed_cost_11.present? %> <th><%= @household_account_book.fixed_cost_11 %></th> <% end %> <% if @household_account_book.fixed_cost_12.present? %> <th><%= @household_account_book.fixed_cost_12 %></th> <% end %> <% if @household_account_book.fixed_cost_13.present? %> <th><%= @household_account_book.fixed_cost_13 %></th> <% end %> <% if @household_account_book.fixed_cost_14.present? %> <th><%= @household_account_book.fixed_cost_14 %></th> <% end %> <th>備考</th> </tr> </thead> <tbody> <!--この@household_account_booksは、application.html.erbのset_one_monthに定義されている--> <% @household_account_books.each do |day| %> <tr> <td><%= l(day.worked_on, format: :short) %></td> <td><%= $days_of_the_week[day.worked_on.wday] %></td> <% if @household_account_book.fixed_cost.present? %> <td><%= day.fixed_n %></td> <% end %> <% if @household_account_book.fixed_cost_1.present? %> <td><%= day.fixed_n_1 %></td> <% end %> <% if @household_account_book.fixed_cost_2.present? %> <td><%= day.fixed_n_2 %></td> <% end %> <% if @household_account_book.fixed_cost_3.present? %> <td><%= day.fixed_n_3 %></td> <% end %> <% if @household_account_book.fixed_cost_4.present? %> <td><%= day.fixed_n_4 %></td> <% end %> <% if @household_account_book.fixed_cost_5.present? %> <td><%= day.fixed_n_5 %></td> <% end %> <% if @household_account_book.fixed_cost_6.present? %> <td><%= day.fixed_n_6 %></td> <% end %> <% if @household_account_book.fixed_cost_7.present? %> <td><%= day.fixed_n_7 %></td> <% end %> <% if @household_account_book.fixed_cost_8.present? %> <td><%= day.fixed_n_8 %></td> <% end %> <% if @household_account_book.fixed_cost_9.present? %> <td><%= day.fixed_n_9 %></td> <% end %> <% if @household_account_book.fixed_cost_10.present? %> <td><%= day.fixed_n_10 %></td> <% end %> <% if @household_account_book.fixed_cost_11.present? %> <td><%= day.fixed_n_11 %></td> <% end %> <% if @household_account_book.fixed_cost_12.present? %> <td><%= day.fixed_n_12 %></td> <% end %> <% if @household_account_book.fixed_cost_13.present? %> <td><%= day.fixed_n_13 %></td> <% end %> <% if @household_account_book.fixed_cost_14.present? %> <td><%= day.fixed_n_14 %></td> <% end %> <% if @household_account_book.fixed_cost_15.present? %> <td><%= day.fixed_n_15 %></td> <% end %> <td><%= day.note %></td> </tr> <% end %> </tbody> </table> </div> <div> <table class="table table-bordered table-condensed table-hover" id="table-attendances"> <tfoot> <tr> <td><%= "合計" %></td> <% @array = @household_account_book.fixed_n.to_i + @household_account_book.fixed_n_1.to_i + @household_account_book.fixed_n_2.to_i + @household_account_book.fixed_n_3.to_i %> <td><%= "#{@array} 円" %></td> </tr> </tfoot> </table> </div>

試したこと

ユーザーコントローラのshowアクションにfind_byで@first_day..@last_dayまでの日にちのカラムのデータを取ろうとするも変化なし。
ご教授下さい。よろしくお願い致します。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

winterboum

2021/01/28 12:57

「各カラムの合計値」をわかりやすく説明してくd祭。 なんとなくは分かるのですが、なんとなくをはっきりした形に理解するのに疲れるので。 どういうデータを集めて、そのどの項目を集計するのか
sh11821783

2021/01/28 13:25

お返事有難うございます。 初めての投稿というのもあり言葉足らずで申し訳ございません。 <各項目の詳細> fixed_cost_1, fixed_cost_2、、、、、(電気代や、水道代などの各項目) <各カラムの詳細> fixed_n、fixed_n_1、fixed_n_2、、、、(fixed_cost_1が例えば、水道代なら、それに対してのfixed_n_1は金額が入ります) そして、どういうデータを集めたいのか?に関しては、 「1日や2日、3日と日毎の各金額(fixed_nや、fixed_n_1)の合計」を集計したいと考えております。 すいません、初心者なもので伝えきれているか不安ではありますが、、
sh11821783

2021/01/28 13:29

<%= "#{@array} 円" %>に「各fixed_n(金額)」の合計を出したいのですが、1日の各fixed_n(金額)」しか出てきてくれません。
sh11821783

2021/01/30 01:58

お世話になっております。 すいません、追記気づきませんでした!追記を見てもう一度考えてみます。
guest

回答1

0

SQLのベテランならDBアクセスで集計値を得られるかも、、、ですが
それは私ではないし、「高度なSQLよりもまずはrubyを学んでもらおう」ということで
viewのcodeは長いんでまだきちんと読んでませんが、とりあえず1日分しか取れないことを解決します。
@household_account_books = @user.household_account_books. where(worked_on: @first_day..@last_day)
取り込む変数の変数名は複数形にしましょう。

「@household_account_booksは、application.html.erbのset_one_monthに定義されている」
そんなところでやっちゃいけません。

find_byは一致するレコードの最初の一つしか返しません。whereに。

@household_account_books.each do |day| の day は HouseholeAccountBookのインスタンスですからhousehold_account_bookにしましょう。読む人が勘違いしないように。

追記
たとえば
@household_account_books.inject(0){|sum, household_account_book| sum + household_account_book.fixed_cost_1.to_i }
to_i してるのは、データのない日に 0 でなく nil が入っているかのうせいがあるから

投稿2021/01/28 23:32

編集2021/01/29 12:06
winterboum

総合スコア23401

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sh11821783

2021/01/29 07:58 編集

返信遅くなり申し訳ございません。 @household_account_books = @user.household_account_books.where(worked_on:@first_day..@last_day) をusersコントローラーのshowアクションに定義して見たのですが、ここからどのようにしてfixed_n(金額)を合計すべきでしょうか? たくさんあるパラメータのうちgroup_byなど用いて作ろうとしましたが上手くいきません。ご指導よろしくお願い致しましす。
sh11821783

2021/01/30 05:27

ご回答有難うございます。 『「@household_account_booksは、application.html.erbのset_one_monthに定義されている」 そんなところでやっちゃいけません。』 この部分が理解できないので教えていただければ助かります。
winterboum

2021/01/30 07:47

1. layoutはシステム全体で使うものですから、ある特定のactiomで使うものを置くのは良くないです 2. データを整理するのはmodel、controllerにやらせ、viewはV(見せる)ことに専念するのが良いです。 model、controllerで出来るのならそちらでやりましょう
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問