In my Donations Model, I have monetized the amount_cents column
I need the humanized version of the donation amount (Eg. 643.50) in the Model so I can generate and send a PDF receipt to the donor.
I've tried humanized_money(self.amount) and self.amount.humanized_money but get the error :
NoMethodError: undefined method `humanized_money' for #<Donation:0x007fa687ebb678>
How can I get this humanized form in the Models?
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
Below is the database schema :
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
To do this without loading everything in ActionView::Base
. You can just do
return ActionController::Base.helpers.humanized_money sum
Including ActionView::Base will add a LOT of extra stuff to your model, do NOT do it this way.
Do this instead:
include MoneyRails::ActionViewExtension
This way you get all (5 at the time of this post) of the money view helper methods.
NoMethodError: undefined method `humanized_money' for #<Donation:0x007fa687ebb678>
From the error message its clear that you are calling the humanized_money
helper method for a Donation
object, NOT a Money
object. That's why its failing.
If you monetized the amount_cents
column properly already, then automagically, your amount
column will be of Money
type i.e. Money
object which you can pass to the humanized_money
helper method as parameter like this:
humanized_money amount
I would say, check the type of your amount
and make sure its a Money
object which it should be if you properly monetized amount_cents
column of your Donation
model. That's why it's not working in this case.
Looks like humanized_money
is defined in the action_view_extension
of the money-rails gem and expected to work in views only. Not in model.
A possible solution to this problem would be to include ActionView::Base module in the Model, that would make the humanized_money
method available inside the model. And then you can call humanized_money in your model like:
include ActionView::Base
humanized_money(sum)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With