0

这是我的代码:

require 'date'

FactoryGirl.define do
  factory :construction_site do
    startdate = Date.today - rand(500)
    enddate = startdate + (1 + rand(1000))
    name { Faker::Lorem.words(2) }
    address {Faker::Address.street_address + "\n" + Faker::Address.zip_code + " " + Faker::Address.city} 
    internalnumber { "CON" + ( 1 + rand(50000)) }
    self.begin { startdate }
    self.end { enddate }
    finished { enddate < Date.today }
    user
    customer
  end
end

我得到这个错误:

/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.2/lib/active_support/core_ext/
date/calculations.rb:100:in `-': expected numeric (TypeError)
        from /Users/kannix/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.2/lib/active_supp
ort/core_ext/date/calculations.rb:100:in `minus_with_duration'
        from /Users/kannix/railsapp/spec/factories/construction_sites.rb:5
:in `block (2 levels) in <top (required)>'
        from /Users/kannix/.rvm/gems/ruby-1.9.3-p125/gems/factory_girl-4.1.0/lib/factory_girl
/syntax/default.rb:18:in `instance_eval'
        from /Users/kannix/.rvm/gems/ruby-1.9.3-p125/gems/factory_girl-4.1.0/lib/factory_girl
/syntax/default.rb:18:in `factory'
        from /Users/kannix/railsapp/spec/factories/construction_sites.rb:4
:in `block in <top (required)>'
        from /Users/kannix/.rvm/gems/ruby-1.9.3-p125/gems/factory_girl-4.1.0/lib/factory_girl
/syntax/default.rb:49:in `instance_eval'
        from /Users/kannix/.rvm/gems/ruby-1.9.3-p125/gems/factory_girl-4.1.0/lib/factory_girl
/syntax/default.rb:49:in `run'
        from /Users/kannix/.rvm/gems/ruby-1.9.3-p125/gems/factory_girl-4.1.0/lib/factory_girl
/syntax/default.rb:7:in `define'
        from /Users/kannix/railsapp/spec/factories/construction_sites.rb:3
:in `<top (required)>'

我的错误在哪里,该代码中是否还有其他需要更改的内容?(我是 Rails 新手;))

4

3 回答 3

1

尝试更改以下行:

startdate = Date.today - rand(500)
enddate = startdate + (1 + rand(1000))

至:

startdate { Date.today - rand(500).days }
enddate { startdate + (1 + rand(1000)).days }

添加.days将保留startdateenddate作为Date对象

于 2012-09-20T15:09:32.860 回答
1

在工厂声明之前尝试分配这些值怎么样?

FactoryGirl.define do
  startdate = Date.today - rand(500)
  enddate = startdate + (1 + rand(1000))

  factory :construction_site do
    name { Faker::Lorem.words(2) }
    address {Faker::Address.street_address + "\n" + Faker::Address.zip_code + " " + Faker::Address.city} 
    internalnumber { "CON" + ( 1 + rand(50000)) }
    self.begin { startdate }
    self.end { enddate }
    finished { enddate < Date.today }
    user
    customer
  end
end

甚至在第一行之前。

于 2012-09-20T19:27:32.810 回答
0

我这样解决了

require 'date'

FactoryGirl.define do
  startdate = Date.today + 100
  factory :construction_site do
    name { Faker::Lorem.words(2) }
    address {Faker::Address.street_name + ' ' + rand(1000).to_s + "\n" + Faker::Address.zip_code + ' ' + Faker::Address.city} 
    internal_number { "CON" + ( 1 + rand(50000)).to_s }
    self.begin { startdate - rand(500) }
    self.end { startdate + rand(1000) }
    finished { self.end < Date.today }
    user
    customer
  end
end
于 2012-09-21T11:40:47.163 回答