家計簿アプリ開発中です。
やりたいこと:
今月の現時点の収支状況を表示させたい。
流れ、イメージ
income(収入)テーブルのdateカラムから2019年6月(つまり今月)のレコードを条件とし、amount(金額)カラムの合計値を取得したい。
現在の状況:
{5月の合計金額、6月の合計金額}として取得できています。
ここから6月(今月)の値のみ取得したい。
def index @name = current_user.name time = Time.now this_month = time.month @income_budget = Budget.find_by(month: this_month).income_amount @spending_budget = Budget.find_by(month: this_month).spending_amount 以下問題箇所(月ごとには取得できるが「今年の今月のみ」を条件として取得したい) @current_income = Income.group("YEAR(date)").group("MONTH(date)").select(date: this_month).sum(:amount) binding.pry end
ターミナル
1165000のみ取得して表示させたい
[1] pry(#<BooksController>)> @current_income => {[2019, 5]=>150000, [2019, 6]=>1165000}
ビュー
.current_income 現在の状況 = @current_income.to_s(:delimited)