【问题标题】:Cannot create records from rails seeds, fails with undefined method `generated_methods?'无法从 Rails 种子创建记录,因未定义的方法“generated_methods?”而失败?
【发布时间】:2015-09-18 12:12:52
【问题描述】:

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

当我运行 rake 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>'

【问题讨论】:

  • 模型上定义了哪些属性?
  • @japed 我编辑了我的问题。
  • @Зелёный 不,他们不是,你可以这样做 find_or_create_by_code rails 3 guides 但在 Rails 4 中它将是 find_or_create_by(code: "blah")
  • @japed 我看到种子库 gem 已安装。这会导致它吗?
  • 我不确定,但看看apidock,该方法已被弃用,因此在 rails 3 中不存在。我肯定会尝试删除种子库 gem,然后看看它是否有效。

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-2


【解决方案1】:

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

尝试升级到 3.x 版:

在您的 Gemfile 中

gem 'validates_timeliness', '~> 3.0'

然后,照常运行:

$ bundle install

【讨论】:

  • 太好了,它来自 gem 'szilm-validates_timeliness',它需要 'validates_timeliness' gem。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
  • 2014-05-13
  • 2019-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多