初始化貨幣化領域採取以下車型爲例:錢軌道:不0
class Product < ApplicationRecord
monetize :price_cents, numericality: { greater_than_or_equal_to: 0 }
…
}
這種模式
create_table "products", id: :serial, force: :cascade do |t|
t.integer "price_cents", null: false
…
end
當我創建一個新的Product
有零price
而不是值爲零我得到一個值爲0的Money
對象。對我的用例不合意,因爲我寧願將空值設置爲顯式而非隱式設置。
設置在config.amount_column
default:nil
只給我上初始化所期望的行爲,但隨後price
得到,如果Product
與price
更新仍設置爲無設置爲0 Money
對象。
如何阻止此行爲?
不應該是't.integer「price_cents」,null:true'? – everyman
對,當我爲了簡潔而修改示例時犯了一個錯誤。它實際上只是'price_cents'。 – ESWAT