Money-Rails Gem: Humanized_money 模型
Money-Rails Gem: Humanized_money in Models
在我的捐赠模型中,我已通过 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
NoMethodError: undefined method `humanized_money' for #<Donation:0x007fa687ebb678>
从错误消息中可以清楚地看出,您正在为 Donation
对象而不是 Money
对象调用 humanized_money
辅助方法。这就是它失败的原因。
如果您已经正确地将 amount_cents
列货币化,那么您的 amount
列将自动成为 Money
类型,即 Money
对象,您可以将其传递给humanized_money
像这样作为参数的辅助方法:
humanized_money amount
我想说的是,检查你的 amount
的类型并确保它是一个 Money
对象,如果你正确地将 Donation
的 amount_cents
列货币化,它应该是] 模型。这就是为什么它在这种情况下不起作用。
更新
看起来 humanized_money
是在 money-rails gem 的 action_view_extension
中定义的,预计只能在视图中使用。不在模型中。
此问题的可能解决方案是在模型中包含 ActionView::Base 模块,这将使 humanized_money
方法在模型中可用。然后您可以在您的模型中调用 humanized_money,例如:
include ActionView::Base
humanized_money(sum)
KM Rakibul 关于助手未包含在 ActiveRecord 中的说法是正确的,但它们与 ActionView::Base 挂钩,因此要包含它们,请使用:
include ActionView::Base
无需加载 ActionView::Base
中的所有内容即可执行此操作。你可以做
return ActionController::Base.helpers.humanized_money sum
包括 ActionView::Base 会给你的模型添加很多额外的东西,不要这样做。
改为这样做:
include MoneyRails::ActionViewExtension
通过这种方式,您可以获得所有(post 时的 5 个)货币视图帮助器方法。
不需要在您的模型中包含助手。
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)
在我的捐赠模型中,我已通过 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
NoMethodError: undefined method `humanized_money' for #<Donation:0x007fa687ebb678>
从错误消息中可以清楚地看出,您正在为 Donation
对象而不是 Money
对象调用 humanized_money
辅助方法。这就是它失败的原因。
如果您已经正确地将 amount_cents
列货币化,那么您的 amount
列将自动成为 Money
类型,即 Money
对象,您可以将其传递给humanized_money
像这样作为参数的辅助方法:
humanized_money amount
我想说的是,检查你的 amount
的类型并确保它是一个 Money
对象,如果你正确地将 Donation
的 amount_cents
列货币化,它应该是] 模型。这就是为什么它在这种情况下不起作用。
更新
看起来 humanized_money
是在 money-rails gem 的 action_view_extension
中定义的,预计只能在视图中使用。不在模型中。
此问题的可能解决方案是在模型中包含 ActionView::Base 模块,这将使 humanized_money
方法在模型中可用。然后您可以在您的模型中调用 humanized_money,例如:
include ActionView::Base
humanized_money(sum)
KM Rakibul 关于助手未包含在 ActiveRecord 中的说法是正确的,但它们与 ActionView::Base 挂钩,因此要包含它们,请使用:
include ActionView::Base
无需加载 ActionView::Base
中的所有内容即可执行此操作。你可以做
return ActionController::Base.helpers.humanized_money sum
包括 ActionView::Base 会给你的模型添加很多额外的东西,不要这样做。
改为这样做:
include MoneyRails::ActionViewExtension
通过这种方式,您可以获得所有(post 时的 5 个)货币视图帮助器方法。
不需要在您的模型中包含助手。
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)