以下问答基于 Trailblazer book pp. ~50-60 中给出的示例,适用于我的具体要求。如果按照书中的说明进行操作,您可以简单地将ARInvoice
和ar_invoice
视为Thing
并获得一般漂移。thing
我的operation.rb
文件是:
class ARInvoice < GLTransaction class Create <
Trailblazer::Operation
include( Model )
model( ARInvoice, :create )
contract() do
property( :invoice_number )
property( :currency_code )
property( :forex_rate )
property( :gl_account_id )
validates( :invoice_number, :presence => true )
validates( :currency_code, :presence => true )
end
def process( params )
@model = ARInvoice.new # have to use instance variable here
validate( params[ :ar_invoice ], @model ) do |f|
f.save
end
end
end
end
我从开拓者的书中改编了这个测试:
it("INSERTs a valid invoice") do
test_time = Time.now
puts(test_time)
ar_invoice = ARInvoice::Create.(
:ar_invoice => {
:invoice_number => 101,
:gl_account_id => 1,
:effective_from => test_time.to_s
}
).model
ar_invoice.persisted?.must_equal(true)
ar_invoice.invoice_number.must_equal(101)
ar_invoice.transaction_type.must_equal('IN')
ar_invoice.effective_from.must_equal(test_time)
ar_invoice.superseded_after.must_equal(nil)
end
我得到了这个错误:
ar_invoice crud Create#test_0001_INSERTs a valid invoice: \
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: \
gl_transactions.effective_from may not be NULL: \
INSERT INTO "gl_transactions" . . .
但我也看到了这个:
# Running:
2016-02-08 11:24:35 -0500
E
因此,设置了 test_time 值。为什么它没有进入effective_from
属性?
我在下面给出的答案。