已将money-rails
gem 添加到我的应用程序中。
将其列迁移到我的模型Item
移民
class AddPriceToItems < ActiveRecord::Migration[5.0]
def change
add_column :items, :price, :money
end
end
模型项目
class Item < ApplicationRecord
belongs_to :invoice
validates_numericality_of :unit_price, :quantity
monetize :price_cents
end
架构
ActiveRecord::Schema.define(version: 20170223211329) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "invoices", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "items", force: :cascade do |t|
t.decimal "unit_price", precision: 10, scale: 2
t.integer "quantity"
t.integer "invoice_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.money "price", scale: 2
t.index ["invoice_id"], name: "index_items_on_invoice_id", using: :btree
end
add_foreign_key "items", "invoices"
end
这是我遇到的错误
undefined method `price_cents' for #<Item:0x007f9f6b366ae8>
Did you mean? price_cents=
不知道如何解决这个问题。
编辑
好的,我删除了价格列,并将其添加回 price_cents 列作为整数...
但是,在架构中应该是这样的:
t.integer "price_cents"
..
或者
t.monetize "price_cents"