我正在学习一个教程,在一个新的项目中使用money。
下面是我的迁移文件:
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我的模特:
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我读过所有关于money的API规范,但我想不太明白。
如果我运行rails控制台并执行Plan.last.price,它将显示以下错误:
.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我在这里做错了什么?如何为这个价格属性设置一个值?
谢谢
发布于 2018-09-27 15:18:29
查看有关“`money rails”的教程,您将看到他们推荐的迁移是
add_monetize :products, :price # Rails 4x and above它实际上在模型中创建了一个名为price_cents的整数字段。
您需要进行另一次迁移来删除price,然后使用上面的行将price_cents添加到表中。
https://stackoverflow.com/questions/52540101
复制相似问题