【问题标题】:Rails 5.1 different sum calculation with multiple currenciesRails 5.1 多种货币的不同总和计算
【发布时间】:2018-01-21 22:05:08
【问题描述】:

我正在开发 Rails 应用程序以支付费用。
基本上我会输入一些费用,给出名称、描述、金额、货币和日期。
为此,我有 3 个模型:用户、支出、货币与此关系:

class Currency < ApplicationRecord

    has_many :spendings
    has_many :users, through: :spendings

end

class Spending < ApplicationRecord

    belongs_to :user
    belongs_to :currency

end

class User < ApplicationRecord

  has_many :spendings
  has_many :currencies, through: :spendings

end

我正在使用 users_controller 中的节目来索引每个用户的费用。这就是我的 users_controller 中的内容

class UsersController < ApplicationController

    def show
        @user_spendings = @current_user.spendings.all.order('date DESC').paginate(:page => params[:page], :per_page => 10)
        @user_amount = @user_spendings.sum(:amount)
    end
end

在这种情况下,@user_amount 向我显示了 current_user 的所有费用,问题是我有 2 种货币(将来可能会更多),我想显示不同的总金额,具体取决于何时选择了哪种货币我创建了一项新费用。
我在这里考虑了不同的事情,我尝试做一个 If 语句,以便仅当 currency_id == 为 1 (如果€)或 2 (如果 $ 等)时显示金额...但是如果我添加新货币(和我无法让它工作)。
也许是一个循环?循环浏览货币​​并以某种方式显示总金额。但是你不能循环求和所以我不知道。
我也希望它灵活,所以如果将来我添加更多货币,我不必接触代码。
有什么想法吗?
如果需要这里是我的 show.html.erb

  <% @user_spendings.each do |spending| %>
  <tr>
    <td><%= spending.title %></td>
    <td><%= spending.description %></td>
    <td><%= '%.02f' % spending.amount %></td>
    <td><%= spending.currency.symb %></td>
    <td><%= spending.date.strftime('%d %B %Y') %></td>
  </tr>
  <% end %>

非常感谢。

【问题讨论】:

  • 我发现了类似的东西:Spending.where(id: Currency.where(name: 'Euro').select(:spending_id)).sum(:amount) 但它显示我为 0结果:(

标签: ruby-on-rails sum ruby-on-rails-5 currency calculation


【解决方案1】:

按照以下方式做某事:

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

将允许您迭代货币,如下所示:

@sums_by_currency.each do |currency|
  "#{currency.symb}#{currency.amount}"
end

@sums_by_currency.each do |currency|
  number_to_currency(currency.amount, unit: currency.symb)
end

在你看来。

【讨论】:

  • 嘿,我也试过你的方法。它可以工作,但它在一行上显示了欧元和美元的总金额:€.-40.00。基本上它总结了所有的金额
  • 只是确保您使用了更新的视图代码 - 我不久前更新了它。
  • 您是说您有两项费用,一项价值 26 欧元,一项价值 14 美元,而视图只是吐出 40 欧元?
  • 啊,刚刚注意到我从未添加过该组。修改查询以添加group 子句
  • 您好,很抱歉打扰,但我遇到了其他问题。这是一个不同的问题,但它与你给我的代码有关。也许您可以循环并再次帮助我? HERE。会很酷,很抱歉打扰...:/
【解决方案2】:

如果您已经设置了表之间的关系,您实际上可以通过执行以下操作来访问所有属于CurrencySpendings

Currency.all.each do |c|
  c.spendings.sum(&:amount)
end

或者对于每一种货币,您都可以这样做:

Currency.first.spendings.all

首先检查所有费用Currency

Currency.first.spendings.sum(&:amount)

查看第一Currency

的所有费用总和

当然,对于所有其他具有has_many - belongs_to 关系的模型都可以这样做

【讨论】:

  • 您好,感谢您的回答。第一个有效。第二个我在页面上收到此错误:#<:activerecord_associationrelation:0x6835590>,第三个页面没有加载并向我显示:#<0xb4a2b10>
  • &lt;&gt;
  • <:activerecord_associationrelation:0xa057158><:activerecord_associationrelation:0xb1b03d0>
【解决方案3】:

我知道还有其他答案,但这是我的方法。

控制器

class UsersController < ApplicationController

  def show
    @user = current_user
    @spendings = @user.spendings
    @currencies = @user.currencies
  end

end

型号

class Currency < ApplicationRecord

  has_many :spendings
  has_many :users, through: :spendings

  def user_spendings user
    spendings.where(user_id: user.id)
  end

  def total_user_spendings user
    user_spendings(user).sum(:amount)
  end

end

查看

<h2>Spendings summary by currency</h2>
<table>
  <% @currencies.each do |currency| %>
    <tr>
      <td><%= currency.symb %></td>
      <td><%= currency.name %></td>
      <td><%= '%.02f' % currency.total_user_spendings @user %></td>
    </tr>
  <% end %>
</table>

<h2>Detailed spendings</h2>
<table>
  <% @spendings.each do |spending| %>
    <tr>
      <td><%= spending.title %></td>
      <td><%= spending.description %></td>
      <td><%= '%.02f' % spending.amount %></td>
      <td><%= spending.currency.symb %></td>
      <td><%= spending.date.strftime('%d %B %Y') %></td>
    </tr>
  <% end %>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2013-04-23
    • 1970-01-01
    • 2012-07-25
    • 2014-07-22
    • 2018-08-31
    • 2019-03-09
    相关资源
    最近更新 更多