Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 问:;A.开拓者测试-合同方法的影响_Ruby On Rails_Ruby_Trailblazer - Fatal编程技术网

Ruby on rails 问:;A.开拓者测试-合同方法的影响

Ruby on rails 问:;A.开拓者测试-合同方法的影响,ruby-on-rails,ruby,trailblazer,Ruby On Rails,Ruby,Trailblazer,以下问答基于《开拓者》一书第50-60页中给出的示例,并根据我的具体要求进行了调整。你可以简单地把ARInvoice和ar\u invoice想象成Thing和Thing,如果你在书中这样做的话,就可以得到一般的大意 我的operation.rb文件是: class ARInvoice < GLTransaction class Create < Trailblazer::Operation include( Model ) model( ARInvoice

以下问答基于《开拓者》一书第50-60页中给出的示例,并根据我的具体要求进行了调整。你可以简单地把
ARInvoice
ar\u 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
我得到了这个错误:

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
因此,设置了测试时间值。为什么它没有从属性进入
有效_


我在下面给出了答案。

问题是我没有理解《开拓者》(TBR)中使用的
合同
do/end
块的意义。我最初读这本书的方式是合同与形式有关。我在RoR方面的经验使我将单词形式映射到单词视图。我的错误。但也可能绊倒其他人

正确的合同块包含缺少的属性:
effective\u from

contract() do
  property( :invoice_number )
  property( :currency_code )
  property( :forex_rate )
  property( :gl_account_id )

  property( :effective_from )

  validates( :invoice_number, :presence => true )
  validates( :currency_code, :presence => true )
end
现在,如果我将单词
form
映射到
params[:model]
,它会帮助我。因为当调用
过程
方法时,在
参数[:model]
散列中查找的属性仅限于
合同
执行/结束
块中明确列出的那些属性;这就是为什么在使用TBR时不需要强参数的原因

请注意,原始测试仍然失败,因为未满足
currency\u code
验证。但在编写此测试时,这是有意的

contract() do
  property( :invoice_number )
  property( :currency_code )
  property( :forex_rate )
  property( :gl_account_id )

  property( :effective_from )

  validates( :invoice_number, :presence => true )
  validates( :currency_code, :presence => true )
end