用于迁移的未定义方法find_each - 第二次运行

时间:2015-01-21 09:24:53

标签: ruby-on-rails ruby ruby-on-rails-3 migration rails-activerecord

当我运行以下迁移时:“20150121081105_update_interest_group_entries.rb”

class UpdateInterestGroupEntries < ActiveRecord::Migration
  def change
    InterestGroupTranslation.find_each do |itt|
      if itt.alias.blank?
        itt.alias = "alias_interest_group_" + Random.rand(100).to_s + itt.locale.to_s
        itt.save!
      end
    end

    PageTranslation.find_each do |page|
      if page.alias.blank?
        page.alias = "alias_page_" + Random.rand(100).to_s + page.locale.to_s
        page.save!
      end
    end

    TestimonialTranslation.find_each do |test|
      if test.alias.blank?
        test.alias = "alias_test_" + Random.rand(100).to_s + test.locale.to_s
        test.save!
      end
    end

    TestimonialInternalTranslation.find_each do |test_internal|
      if test_internal.alias.blank?
        test_internal.alias = "alias_test_internal_" + Random.rand(100).to_s + test_internal.locale.to_s
        test_internal.save!
      end
    end

    InterestGroupInternalTranslation.find_each do |ittinternal|
      if ittinternal.alias.blank?
        ittinternal.alias = "alias_ittinternal_" + Random.rand(100).to_s + ittinternal.locale.to_s
        ittinternal.save!
      end
    end
  end
end

它返回此错误:

==  UpdateInterestGroupEntries: migrating =====================================
-- find_each()
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

undefined method `find_each' for #<ActiveRecord::Migration:0x007f98f8ca0e98>/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:465:in `block in method_missing'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:438:in `block in say_with_time'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:438:in `say_with_time'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:458:in `method_missing'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:334:in `method_missing'
/Users/Vincent/Documents/remsis/db/migrate/20150121081105_update_interest_group_entries.rb:31:in `change'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:407:in `block (2 levels) in migrate'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:407:in `block in migrate'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:389:in `migrate'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:528:in `migrate'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:775:in `call'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/transactions.rb:208:in `transaction'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:775:in `ddl_transaction'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:719:in `block in migrate'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:700:in `each'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:700:in `migrate'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:570:in `up'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/migration.rb:551:in `migrate'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/gems/activerecord-3.2.19/lib/active_record/railties/databases.rake:193:in `block (2 levels) in <top (required)>'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/bin/ruby_executable_hooks:15:in `eval'
/Users/Vincent/.rvm/gems/ruby-1.9.3-p550/bin/ruby_executable_hooks:15:in `<main>'

所以它在以下几行失败:

InterestGroupInternalTranslation.find_each do |ittinternal|
  if ittinternal.alias.blank?
    ittinternal.alias = "alias_ittinternal_" + Random.rand(100).to_s + ittinternal.locale.to_s
    ittinternal.save!
  end
end

当我在Console中运行相同的行时,它可以正常工作: enter image description here

奇怪的是: 当我第一次在第一次运行“rake db:migrate”之后,它可以作为一个魅力并完成它:

enter image description here

我正在跑步:

ruby​​ 1.9.3p550(2014-10-27修订版48165)[x86_64-darwin13.4.0]

Rails 3.2.19

1 个答案:

答案 0 :(得分:2)

您可能有另一个名为InterestGroupInternalTranslation的迁移,并且已加载了一个。该迁移的类名与模型相同。 Rails为未迁移的迁移类加载迁移类。这就是为什么当你运行所有迁移时它会给你一个错误,因为它在迁移类上找不到find_each。再次运行时,它将仅运行上次迁移并正确使用模型类。您应该重命名迁移类和文件名,但不要更改时间戳