Ruby on rails FactoryGirl与时间和日期时间有关

Ruby on rails FactoryGirl与时间和日期时间有关,ruby-on-rails,ruby,datetime,time,Ruby On Rails,Ruby,Datetime,Time,我似乎在用FactoryGirl创建时间对象时遇到了一些问题。当我这样做时: factory :invite do |n| fromtime Time.now + 1.weeks totime Time.now + 1.weeks + 1.hour end factory :invite do |n| fromtime Time.now + n.weeks totime Time.now + n.weeks + 1.hour end 工厂运作良好,但我希望有许

我似乎在用FactoryGirl创建时间对象时遇到了一些问题。当我这样做时:

 factory :invite do |n|
   fromtime Time.now + 1.weeks
   totime Time.now + 1.weeks + 1.hour
 end
 factory :invite do |n|
   fromtime Time.now + n.weeks
   totime Time.now + n.weeks + 1.hour
 end
工厂运作良好,但我希望有许多邀请是顺序。所以当我这样做的时候:

 factory :invite do |n|
   fromtime Time.now + 1.weeks
   totime Time.now + 1.weeks + 1.hour
 end
 factory :invite do |n|
   fromtime Time.now + n.weeks
   totime Time.now + n.weeks + 1.hour
 end
我得到这个错误

/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/core_ext/time/calculations.rb:312:in `+': can't convert FactoryGirl::Declaration::Implicit into an exact number (TypeError)
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/core_ext/time/calculations.rb:312:in `plus_with_duration'
from /Users/jamesstrocel/rails/gameplaydate/spec/factories.rb:24:in `block (2 levels) in <top (required)>'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:18:in `instance_eval'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:18:in `factory'
from /Users/jamesstrocel/rails/gameplaydate/spec/factories.rb:22:in `block in <top (required)>'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in `instance_eval'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in `run'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:7:in `define'
from /Users/jamesstrocel/rails/gameplaydate/spec/factories.rb:1:in `<top (required)>'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `block in load'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:16:in `block in find_definitions'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `each'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl_rails-4.1.0/lib/factory_girl_rails/railtie.rb:26:in `block in <class:Railtie>'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:34:in `call'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:42:in `each'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/application/finisher.rb:59:in `block in <module:Finisher>'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `run'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `each'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/application.rb:136:in `initialize!'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/jamesstrocel/rails/gameplaydate/config/environment.rb:5:in `<top (required)>'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/application.rb:103:in `require'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/application.rb:103:in `require_environment!'
from /Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active\u support/core\u ext/time/calculations.rb:312:在“+”中:无法将FactoryGirl::Declaration::Implicit转换为精确的数字(TypeError)
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active\u support/core\u ext/time/calculations.rb:312:in‘plus\u with_duration’
from/Users/jamestrocel/rails/gameplaydate/spec/factories.rb:24:in'block(2层)in'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:18:in'instance_eval'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:18:in'factory'
from/Users/jamestrocel/rails/gameplaydate/spec/factories.rb:22:in'block-in'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in'instance_eval'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in'run'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:7:in'define'
from/Users/jamestrocel/rails/gameplaydate/spec/factories.rb:1:in`'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active\u-support/dependencies.rb:245:in'load'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active\u support/dependencies.rb:245:in'block in load'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active\u-support/dependencies.rb:236:in'load\u-dependency'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active\u-support/dependencies.rb:245:in'load'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/find_定义。rb:16:in“block in find_定义”
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in'each'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl-4.1.0/lib/factory_girl/find_定义。rb:15:in“find_定义”
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/factory_girl_rails-4.1.0/lib/factory_girl_rails/railtie.rb:26:in'block in'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active\u-support/lazy\u-load\u-hooks.rb:34:in'call'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active\u-support/lazy\u-load\u-hook.rb:34:in'execute\u-hook'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active\u-support/lazy\u-load\u-hooks.rb:43:in'block in-run\u-load\u-hooks'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active\u-support/lazy\u-load\u-hooks.rb:42:in'each'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/activesupport-3.2.13/lib/active\u-support/lazy\u-load\u-hooks.rb:42:in'run\u-load\u-hooks'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/application/finisher.rb:59:in'block in'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:30:in'instance_exec'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:30:in'run'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:55:in`block in run_initializers'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:54:in'each'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/initializable.rb:54:in'run_initializers'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/application.rb:136:in'initialize!'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in'method_missing'
来自/Users/jamestrocel/rails/gameplaydate/config/environment.rb:5:in`'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/application.rb:103:in'require'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/application.rb:103:in'require_environment!'
来自/Users/jamesstrocel/.rvm/gems/ruby-2.0.0-p0@gameplaydate/gems/railties-3.2.13/lib/rails/commands.rb:40:in`'
来自脚本/rails:6:in'require'
来自脚本/rails:6:in`'
如何使用连续时间对象获取多个邀请?

试试:

factory :invite do
 sequence(:fromtime) { |n| Time.now + n.weeks }
 sequence(:totime) { |n| Time.now + n.weeks + 1.hour }
end

希望这有帮助

成功了。万分感谢!