Money-Rails Gem:模型中的Humanized_money

时间:2015-08-19 04:30:29

标签: ruby-on-rails rails-activerecord money-rails

在我的捐款模型中,我已将amount_cents列货币化

我需要模型中的捐赠金额(例如643.50)的人性化版本,以便我可以生成并向捐赠者发送PDF收据。

我已尝试过humanized_money(self.amount)和self.amount.humanized_money,但却收到错误:

NoMethodError: undefined method `humanized_money' for #<Donation:0x007fa687ebb678>

如何在模型中获得这种人性化的形式?

class Donation < ActiveRecord::Base
  belongs_to :donatable, polymorphic: true
  belongs_to :added_by_user, foreign_key: "added_by", class_name: "User"

  store_accessor :details, :method

  monetize :amount_cents

  def donations_this_week(branch_id)
    sum =  Donation.sum(:amount_cents).to_money
    return humanized_money(sum)
  end

  def receipt
    Receipts::Receipt.new(
        id: id,
        product: "GoRails",
        message: "This receipt is to acknowledge that we have received a donation with the below details from #{self.donatable.name}",
        company: {
            name: self.donatable.branch.organization.name,
            address: "#{self.donatable.branch.address_line_1}\n#{self.donatable.branch.address_line_2}\n#{self.donatable.branch.email}\n#{self.donatable.branch.legal_details}",
            email: self.donatable.branch.email,
            logo: self.donatable.branch.organization.logo.url(:medium),
        },
        line_items: [
            ["Date",           created_at.to_s],
            ["Donor Name", self.donatable.name],
            ["Amount",         humanized_money(self.amount)],
            ["Payment Method",     self.method],
        ]
    )
  end
end

以下是数据库架构:

create_table "donations", force: :cascade do |t|
t.string   "donatable_type"
t.integer  "donatable_id"
t.integer  "amount_cents"
t.datetime "created_at",                  null: false
t.datetime "updated_at",                  null: false
t.datetime "date"
t.integer  "added_by"
t.jsonb    "details",        default: {}, null: false

5 个答案:

答案 0 :(得分:3)

要在不加载ActionView::Base中的所有内容的情况下执行此操作。你可以做到

return ActionController::Base.helpers.humanized_money sum

答案 1 :(得分:1)

NoMethodError: undefined method `humanized_money' for #<Donation:0x007fa687ebb678>

从错误消息中明确表示您正在为humanized_money对象调用Donation辅助方法,而不是Money对象。这就是它失败的原因。

如果您已经正确地通过amount_cents列获利,那么您的amount列将是Money类型,即Money对象,您可以将其传递给{humanized_money列1}}辅助方法作为参数如下:

humanized_money amount

我想说,检查amount的类型,如果您Money模型的amount_cents列已正确获利,请确保该Donation对象应该是。这就是为什么它不适用于这种情况。

更新

money-rails gemhumanized_money中定义action_view_extension似乎只能在视图中使用。不在模特中。

这个问题的一个可能的解决方案是在模型中包含ActionView::Base模块,这将使humanized_money方法在模型中可用。然后你可以在你的模型中调用humanized_money,如:

 include ActionView::Base
 humanized_money(sum)

答案 2 :(得分:1)

包含ActionView :: Base会为你的模型添加很多额外的东西,不要这样做。

请改为:

include MoneyRails::ActionViewExtension

通过这种方式,您可以获得money view helper方法的全部(在本文发布时为5)。

答案 3 :(得分:1)

不需要在模型中包含帮助程序。

price_options = {
  :no_cents_if_whole => MoneyRails::Configuration.no_cents_if_whole.nil? ? true : MoneyRails::Configuration.no_cents_if_whole,
  :symbol => true
}
your_money_column.format(price_options)

source code on github

答案 4 :(得分:0)

KM Rakibul对于未包含在ActiveRecord中的助手是正确的,但是它们与ActionView::Base挂钩,所以要包括它们使用:

include ActionView::Base