无法从 rails 种子创建记录,因未定义方法“generated_methods?”而失败

Cannot create records from rails seeds, fails with undefined method `generated_methods?'

我最近将一个应用程序从 rails 2.3.18 升级到 rails 3.2.1,我正在尝试完全迁移它。

当我 运行 耙 db:seeds 时,它失败了:

 undefined method `generated_methods?' for WhateverObject

这是一个非常基本的种子文件的内容:

[ 'MTW', 'GBP', 'USD', 'EUR' ].each do |currency|
  binding.pry
  Currency.find_or_create_by_code(currency)
end

通过 pry,我发现我无法调用任何方法,例如:

Currency.new Currency.create

我也试过:

Currency.where(code: currency).first_or_create

Currency.column_names
=> ["id", "code", "rate"]

这是怎么回事?

堆栈跟踪:

rake aborted!
NoMethodError: undefined method `generated_methods?' for Currency(id: integer, code: string, rate: decimal):Class
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.21/lib/active_record/dynamic_matchers.rb:55:in `method_missing'
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/szilm-validates_timeliness-2.3.1/lib/validates_timeliness/active_record/attribute_methods.rb:46:in `define_attribute_methods_with_timeliness'
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.21/lib/active_record/attribute_methods.rb:168:in `respond_to?'
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.21/lib/active_record/attribute_assignment.rb:81:in `block in assign_attributes'
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.21/lib/active_record/attribute_assignment.rb:78:in `each'
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.21/lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.21/lib/active_record/relation/finder_methods.rb:294:in `block in find_or_instantiator_by_attributes'
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.21/lib/active_record/base.rb:500:in `initialize'
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.21/lib/active_record/relation/finder_methods.rb:293:in `new'
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.21/lib/active_record/relation/finder_methods.rb:293:in `find_or_instantiator_by_attributes'
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.21/lib/active_record/dynamic_matchers.rb:52:in `method_missing'
/home/bogdan/projects/%^&^*&/webapp/db/seeds/currencies.seeds.rb:4:in `block in <top (required)>'
/home/bogdan/projects/$%^%&*/webapp/db/seeds/currencies.seeds.rb:2:in `each'
/home/bogdan/projects/&^*&*&/webapp/db/seeds/currencies.seeds.rb:2:in `<top (required)>'
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:245:in `load'
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:245:in `block in load'
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/bogdan/.rvm/gems/ruby-2.1.2/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:245:in `load'
/home/bogdan/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
/home/bogdan/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'

您的问题似乎是由 outdated validates_timeliness gem(您的版本是 2.3.1)引起的。

尝试升级到版本 3.x:

in your Gemfile

gem 'validates_timeliness', '~> 3.0'

然后,运行 照常:

$ bundle install