【问题标题】:Money-Rails Gem: Humanized_money in ModelsMoney-Rails Gem:模型中的 Humanized_money
【发布时间】:2015-08-19 04:30:28
【问题描述】:

在我的捐赠模型中,我通过 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

【问题讨论】:

  • 您应该发布您的捐赠模型,尤其是您通过金额字段获利的部分。马上,我可以告诉你问题是你在Donation 对象上调用humanized_money。您需要使用Money 对象作为参数调用该方法。
  • 把代码贴在你打电话给humanize_money的地方。我猜你可能正在打电话给humanized_money,而不是money_object
  • 附上模型!谢谢!
  • 您能检查一下您数据库中amount 的列类型吗?
  • 附加数据库架构!

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


【解决方案1】:

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

return ActionController::Base.helpers.humanized_money sum

【讨论】:

    【解决方案2】:

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

    改为这样做:

    include MoneyRails::ActionViewExtension
    

    通过这种方式,您可以获得所有(在本文发布时为 5 个)money view 辅助方法。

    【讨论】:

    • 这个!甚至看到了ActionView::Base.new.humanized_money(10) 的解决方案,尤其是当你有一个普通的 api 并且甚至不使用动作视图时......
    【解决方案3】:
    NoMethodError: undefined method `humanized_money' for #<Donation:0x007fa687ebb678>
    

    从错误消息中可以清楚地看出,您正在为 Donation 对象调用 humanized_money 辅助方法,而不是 Money 对象。这就是它失败的原因。

    如果您已经正确地将amount_cents 列货币化,那么您的amount 列将自动成为Money 类型,即Money 对象,您可以将其作为参数传递给humanized_money 辅助方法,如下所示:

    humanized_money amount
    

    我会说,检查您的 amount 的类型,并确保它是一个 Money 对象,如果您正确地将您的 Donation 模型的 amount_cents 列货币化,它应该是。这就是为什么它在这种情况下不起作用的原因。

    更新

    看起来humanized_money 是在money-rails gemaction_view_extension 中定义的,并且预计只能在视图中使用。不在模型中。

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

     include ActionView::Base
     humanized_money(sum)
    

    【讨论】:

    • 我很确定,当我在控制台上运行 Donation.last.amount 时,我得到:=> #
    • 失败在哪一行? return humanized_money(sum)["Amount", humanized_money(self.amount)]?
    • 好的,你能检查一下吗:puts Donation.sum(:amount_cents).to_money.inspect你看到了什么?
    • (0.4ms) SELECT SUM("donations"."amount_cents") FROM "donations" #&lt;Money fractional:33985700 currency:USD&gt; =&gt; nil
    • 好的,所以它是一个Money 对象。然后我看不出它失败的任何原因 :( 似乎是,由于某种原因,它在 Model 类中不起作用。你可以在你的视图中尝试它吗?我相信它会起作用。
    【解决方案4】:

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

    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

    【讨论】:

      【解决方案5】:

      KM Rakibul 关于帮助者没有被包含在 ActiveRecord 中的说法是正确的,但他们被 ActionView::Base 所吸引,所以要包含他们使用:

      include ActionView::Base
      

      【讨论】:

        猜你喜欢
        • 2015-08-29
        • 1970-01-01
        • 2015-07-20
        • 2016-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-15
        • 1970-01-01
        相关资源
        最近更新 更多