【发布时间】:2017-02-24 19:38:47
【问题描述】:
已将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 列,并将其作为 price_cents 列添加为整数 ...
但是,在架构中应该是这样的:
t.integer "price_cents"..
或
t.monetize "price_cents"
【问题讨论】:
标签: ruby-on-rails ruby money-rails