I was following a tutorial to use money-rails in a new project.
Here is my migration file:
class AddFieldsToPlan < ActiveRecord::Migration[5.1]
def change
add_column :plans, :payment_gateway_plan_identifier, :string
add_column :plans, :price, :integer
add_column :plans, :interval, :integer
add_column :plans, :interval_count,:integer
add_column :plans, :status,:integer
remove_column :plans, :amount
remove_column :plans, :payment_frequency
end
end
And my model:
class Plan < ApplicationRecord
enum status: {inactive: 0, active: 1}
enum interval: {day: 0, week: 1, month: 2, year: 3}
monetize :price_cents
def end_date_from(date = nil)
date ||= Date.current.to_date
interval_count.send(interval).from_now(date)
end
end
I read all the API specification of money-rails but doesnt understand well I guess.
If I run the rails console, and do a Plan.last.price it shows me this error:
.3.4 :001 > Plan.last.price
Plan Load (2.6ms) SELECT "plans".* FROM "plans" ORDER BY "plans"."id" DESC LIMIT $1 [["LIMIT", 1]]
NoMethodError: undefined method `price_cents' for #<Plan:0x007f8ca807f8f0>
Did you mean? price_cents=
from (irb):1
What Im doing wrong here? How can I set up a value for this price attribute?
Thanks
Look at the tutorial for `money-rails' you'll see the migration they recommend is
add_monetize :products, :price # Rails 4x and above
That actually creates an integer field called price_cents
in the model.
You need another migration to remove price
and then use the above line to add the price_cents
to the table.