家計簿を開発中です。
やりたいこと:
ビューで年・月を選択するとカテゴリーごとに合計金額が表示される。
まず、income_categories_id(給与)で試しに処理の流れを作ってみました。
問題:
DBからハッシュ形式で月ごとの合計値は取れましたが、ビューで選択した年・月の分だけが欲しいのですが抽出できません。
def show @name = current_user.name @search_year = params[:year] @search_month = params[:month] #収入計算 @income_detail_saraly = Income.group("YEAR(date)").group("MONTH(date)").where(income_categories_id: 1).sum(:amount) @income_detail_saraly[[@search_year, @search_month]] binding.pry end
まず、 @search_yearには2019が、 @search_monthには5が入っています。
収入計算の部分で、
まず@income_detail_saralyは
=> {[2019, 5]=>400000, [2019, 6]=>500000}の形で取れています。
必要なのは2019年5月のもののみなので、
@income_detail_saraly[[@search_year, @search_month]]としましたが、
nilでした。
ちなみに、
@income_detail_saraly[[2019,5]]とすると40000がちゃんと取れます。
別のコントローラーで、
@current_income[[this_year,this_month]]としてますが値は取れてます。
(前回、質問させていただき解決したものです)
def index @name = current_user.name # 今月は何月か time = Time.now this_month = time.month this_year = time.year # 今月の現在の収支状況 @current_income = Income.group("YEAR(date)").group("MONTH(date)").sum(:amount) @current_income[[this_year,this_month]] # binding.pry end
何が違うのか,書き方も色々変えてみましたが解決せずです。
詳しい方いらっしゃいましたら、ご意見伺いたいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/25 09:55