【发布时间】: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