I'm trying to get the money-rails gem working, and I'm having problems...
Other similar stackoverflow questions are 6 years old.
Here's the product I have the appropriate columns on:
class Transactions < ActiveRecord::Base
belongs_to :user, optional: true
validates :trans_id, uniqueness: true
monetize :price_cents
end
I've got the gem in my Gemfile, and have run bundle install successfully.
When I create a new item and look at it with pry,
create(vendor:"foo",amount:2.6,trans_id:'123cccc')
id: nil,
vendor: "foo",
amount_cents: 260,
amount_currency: "USD",
trans_id: "123cccc",
tax_cents: 150,
total_cents:410,
- How do I work with it in dollar amounts? I.e. I want to add amount_cents to tax_cents for total_cents. amount 2.60 instead of amount_cents: 260,
- Do I need to add a 'composed_of'?
- Also, why is 'cent's in the naming? I thought it was supposed to be removed as the vague documentation states:
In this case the name of the money attribute is created automagically by removing the _cents suffix from the column name.
The
moneygem is storing the the amount in cents and in the table definition, 2 fields will define the property.For e.g., consider having the property
amountinTransaction. Inschema.rbyou will find 2 fields:amount_centsandamount_currency.So, now you will have a
transaction.amountwith a money object in it. With money object you can:3) 'automagically attribute'
Having a migration:
After the migration you can find in schema.rb
What it's saying with
attribute is created automagically by removing the _cents, it means that you can accessamountproperty from theClientclass withclient.amounthaving a money object.