0

我正在按照教程在一个新项目中使用money-rails。

这是我的迁移文件:

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-rails的所有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

我在这里做错了什么?如何为此价格属性设置值?

谢谢

4

1 回答 1

2

查看“money-rails”的教程,您会看到他们推荐的迁移是

add_monetize :products, :price # Rails 4x and above

这实际上创建了一个price_cents在模型中调用的整数字段。

您需要另一个迁移来删除price,然后使用上面的行将其添加price_cents到表中。

于 2018-09-27T15:18:29.843 回答