未定义的迁移方法 find_each - 第二次运行
Undefined method find_each for migration - runs the second time
当我运行以下迁移时:“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
它returns这个错误:
== 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
当我 运行 控制台中的同一行时,它工作正常:
奇怪的是:
当我在第一次之后第二次 运行 "rake db:migrate" 时,它就像一个魅力并完成它:
我是运行宁:
ruby 1.9.3p550(2014-10-27 修订版 48165)[x86_64-darwin13.4.0]
Rails 3.2.19
您可能有另一个名为 InterestGroupInternalTranslation 的迁移并且已加载该迁移。该迁移的 class 名称与模型相同。 Rails 加载未迁移的迁移 classes。这就是为什么当您 运行 所有迁移时它都会给您一个错误,因为它无法在迁移 class 上找到 find_each。当您再次 运行 时,它将仅 运行 最后一次迁移并正确使用模型 class。您应该重命名迁移 class 和文件名,但不要更改时间戳
当我运行以下迁移时:“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
它returns这个错误:
== 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
当我 运行 控制台中的同一行时,它工作正常:
奇怪的是: 当我在第一次之后第二次 运行 "rake db:migrate" 时,它就像一个魅力并完成它:
我是运行宁:
ruby 1.9.3p550(2014-10-27 修订版 48165)[x86_64-darwin13.4.0]
Rails 3.2.19
您可能有另一个名为 InterestGroupInternalTranslation 的迁移并且已加载该迁移。该迁移的 class 名称与模型相同。 Rails 加载未迁移的迁移 classes。这就是为什么当您 运行 所有迁移时它都会给您一个错误,因为它无法在迁移 class 上找到 find_each。当您再次 运行 时,它将仅 运行 最后一次迁移并正确使用模型 class。您应该重命名迁移 class 和文件名,但不要更改时间戳