Mic*_*tor 5 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>
Run Code Online (Sandbox Code Playgroud)
如何在模型中获得这种人性化的形式?
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
Run Code Online (Sandbox Code Playgroud)
以下是数据库架构:
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
Run Code Online (Sandbox Code Playgroud)
要执行此操作而不将所有内容加载到ActionView::Base. 你可以这样做
return ActionController::Base.helpers.humanized_money sum
Run Code Online (Sandbox Code Playgroud)
不需要在您的模型中包含助手。
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)
Run Code Online (Sandbox Code Playgroud)