Rails 5.1,用多种货币绘制一笔总和 [英] Rails 5.1, chartkick a sum with multiple currencies

查看:81
本文介绍了Rails 5.1,用多种货币绘制一笔总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在有关宪章问题上遇到了一个小问题。我有一个铁路应用程序,您可以在其中创建不同的货币。然后,您可以创建带有标题,金额的费用,并从列表和user_id中选择货币。关系已经建立并且正在工作。我的用户控制器中有这样的内容:

I'm stuck on a tiny problem regarding chartkick. I have a rail app where you can create different currencies. You can then create a expense with a title, a amount and choose the currency from a list and the user_id. The relations are made and working. I have in my user controller something like this :

@user_spendings = @current_user.spendings.all.order('date DESC').paginate(:page => params[:page], :per_page => 15)

@sums_by_currency = Currency.joins(:spendings).
  select(:symb, 'SUM(spendings.amount) AS amount').
  where(spendings: { id: @user_spendings.map(&:id) }).
  group(:symb)

在我的显示视图中(因为我希望从每个显示给用户的用户)是这样的:

And in my show view (as I want the expense from each user to be shown there) something like this :

    <% @sums_by_currency.each do |currency| %>
       <%= '%.02f' % "#{currency.amount}" %> <%= "#{currency.symb}" %>
    <% end %>

这显示了每笔支出的总和,具体取决于货币。

我想要使用此总计并使用chartkick来显示支出以及创建支出的日期。

我已经尝试了几件事

首先我去了只是看看:

That shows me the sum for each spending depending on the currency.
I would like to use this total and use chartkick to display the spending, with the date when this spending has been created.
I've tried several things already
First I went with this just to see :

<% @sums_by_currency.each do |currency| %>

  <%= bar_chart currency.amount %>

<% end %>

好吧,我必须要显示图表,但什么也没有显示。也许循环不是解决方案。然后我想到了.map,但我真的不知道该如何将其放置在适当的位置。

我也尝试过:

Well I have to charts appearing but nothing shows up. Maybe the loop isn't the solution. Then I thought about the .map but I don't really know how to put that in place to be honnest.
I tried this aswell :

<%= line_chart  @current_user.spendings.group(:date).sum(:amount)  %> 

这显示了所有货币的总支出。我必须找出如何在不同图表中分割所有货币并仅显示每种货币的总金额。

That shows me the total spendings from all the currencies. I have to find out how to split all the currencies in different charts and show only the total amount from each currency.

如果有人可以给我一个提示,我将不胜感激。

If anyone can give me a clue I would appreciate it.

非常感谢。

推荐答案

好的,我知道了! br>
只花了我2天时间...

对于对此答案感兴趣的人是我的工作。实际上,我没有更改控制器中的任何内容,而是让 @sums_by_currency 像这样。

相反,我去了:

Ok guys I got it !
Took me 2 days only...
For the one interested in the answer here is what I did. I actually didn't change anything in the controller and I let the @sums_by_currency like it is.
Instead I went for that :

<%= column_chart @current_user.spendings.all.joins(:currency).group('currencies.symb').group_by_month(:date, format: "%B %Y").sum(:amount) %>

请给我所有current_user的支出,我从中加入了symb分组的货币。然后我按月对所有内容进行分组,然后从金额中得出总和。

Give me all the spendings from the current_user from where I joined the currency that I grouped by symb. Then I grouped everything by month and I get the sum from the amount.

这篇关于Rails 5.1,用多种货币绘制一笔总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆