2013-05-22 2 views
3

Кажется, у меня возникли проблемы с созданием объекта Time с помощью FactoryGirl. Когда я это делаю:FactoryGirl Проблемы с временем и DateTime

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/[email protected]/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/[email protected]/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/[email protected]/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:18:in `instance_eval' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/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/[email protected]/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in `instance_eval' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in `run' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/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/[email protected]/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `block in load' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:16:in `block in find_definitions' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `each' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/factory_girl_rails-4.1.0/lib/factory_girl_rails/railtie.rb:26:in `block in <class:Railtie>' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:34:in `call' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:42:in `each' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/railties-3.2.13/lib/rails/application/finisher.rb:59:in `block in <module:Finisher>' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `instance_exec' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `run' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/railties-3.2.13/lib/rails/initializable.rb:55:in `block in run_initializers' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `each' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `run_initializers' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/railties-3.2.13/lib/rails/application.rb:136:in `initialize!' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/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/[email protected]/gems/railties-3.2.13/lib/rails/application.rb:103:in `require' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/gems/railties-3.2.13/lib/rails/application.rb:103:in `require_environment!' 
from /Users/jamesstrocel/.rvm/gems/[email protected]/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>' 

Как я могу получить несколько приглашений с последовательными объектами времени?

ответ

5

Try:

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

Надеется, что это помогает!

+0

Это было. Бесконечно благодарен! –

Смежные вопросы