所以......我有一个迁移(post.rb使用state_machine gem)
class AddVerifiedAtToPost < ActiveRecord::Migration
def self.up
add_column :posts, :verified_at, :datetime
Post.find_each do |post|
post.update_attribute(:verified_at, post.created_at) unless post.awaiting_verification?
end
end
def self.down
remove_column :posts, :verified_at
end
end
...这给了我一个未定义的方法“关系”为零类...
所以我想 - 嗯,也许是因为一些奇怪的原因,state_machine谓词方法在迁移的上下文中不起作用,所以我修改了代码:
Post.find_each do |post|
post.update_attribute(:verified_at, post.created_at) unless post.status == "awaiting_verification"
end
但后来我得到重复列的错误“verified_at”已经存在.......好的---那么我将add_column包装在column_exists中?给我一个现在看起来像的迁移:
class AddVerifiedAtToPost < ActiveRecord::Migration
def self.up
unless column_exists?(:posts, :verified_at)
add_column :posts, :verified_at, :datetime
end
Post.find_each do |post|
post.update_attribute(:verified_at, post.created_at) unless post.status == "awaiting_verification"
end
end
def self.down
remove_column :posts, :verified_at
end
end
...运行rake db:migrate ...它成功..万岁...但我仍然持怀疑态度,所以我运行rake db:migrate:down VERSION =
然后再次运行rake db:migrate,猜猜是什么?
== AddVerifiedAtToPost: migrating ============================================
-- column_exists?(:posts, :verified_at)
-> 0.0048s
-- add_column(:posts, :verified_at, :datetime)
-> 0.0644s
rake aborted!
An error has occurred, all later migrations canceled:
undefined method `column' for nil:NilClass
(See full trace by running task with --trace)
...我运行rake db:再次迁移,它的工作原理...我做了rake db:migrate:再次下来,然后rake db:migrate--得到同样的错误........ < / p>
所以我尝试了一个实验..我运行了rake db:migrate:down,删除了列。跳进控制台并执行了ActiveRecord :: Migration.add_column(:posts,:verified_at,:datetime)
迁移迁移 - 没有错误..似乎由于某种原因,在这个迁移文件的上下文中,add_column对我来说都是怪异的。
这是堆栈跟踪:
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
== AddVerifiedAtToPost: migrating ============================================
-- column_exists?(:posts, :verified_at)
-> 0.0052s
-- add_column(:posts, :verified_at, :datetime)
-> 0.0805s
rake aborted!
An error has occurred, all later migrations canceled:
undefined method `column' for nil:NilClass
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/whiny_nil.rb:48:in `method_missing'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/nodes/unqualified_column.rb:8:in `column'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:249:in `visit_Arel_Nodes_Assignment'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in `send'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in `visit'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/mysql.rb:25:in `visit_Arel_Nodes_UpdateStatement'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/mysql.rb:25:in `map'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/mysql.rb:25:in `visit_Arel_Nodes_UpdateStatement'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in `send'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in `visit'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:5:in `accept'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:18:in `accept'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:111:in `with_connection'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:16:in `accept'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/tree_manager.rb:20:in `to_sql'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/crud.rb:20:in `update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/persistence.rb:255:in `update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/locking/optimistic.rb:77:in `update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/attribute_methods/dirty.rb:68:in `update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/timestamp.rb:60:in `update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/callbacks.rb:281:in `update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:419:in `_run_update_callbacks'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/callbacks.rb:281:in `update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/persistence.rb:246:in `create_or_update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/callbacks.rb:273:in `create_or_update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:456:in `_run_save_callbacks'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:221:in `_conditional_callback_around_270'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/integrations/active_record.rb:383:in `around_save'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:149:in `run_actions'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:169:in `catch_exceptions'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:147:in `run_actions'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:132:in `run_callbacks'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:211:in `run_callbacks'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:63:in `perform'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:63:in `catch'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:63:in `perform'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:185:in `within_transaction'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:62:in `perform'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/integrations/active_record.rb:383:in `around_save'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:315:in `send'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:315:in `_callback_around_268'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:220:in `_conditional_callback_around_270'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:414:in `_run_save_callbacks'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/callbacks.rb:273:in `create_or_update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/persistence.rb:39:in `save'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/validations.rb:43:in `save'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/attribute_methods/dirty.rb:21:in `save'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/transactions.rb:240:in `save_without_unsaved_flag'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/transactions.rb:292:in `with_transaction_returning_status'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/transactions.rb:207:in `transaction_without_trace_ActiveRecord_self_name_transaction'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/bundler/gems/rpm-f41cc20ce8b6/lib/new_relic/agent/method_tracer.rb:491:in `transaction'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/bundler/gems/rpm-f41cc20ce8b6/lib/new_relic/agent/method_tracer.rb:242:in `trace_execution_scoped'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/bundler/gems/rpm-f41cc20ce8b6/lib/new_relic/agent/method_tracer.rb:486:in `transaction'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/transactions.rb:240:in `save_without_unsaved_flag'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/transactions.rb:251:in `rollback_active_record_state!'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/transactions.rb:239:in `save_without_unsaved_flag'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/active_scaffold_vho-3.0.20/lib/active_scaffold/extensions/unsaved_record.rb:15:in `save'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/persistence.rb:117:in `update_attribute'
/Users/patrick/coding/rails/bounty_buy/myapp-v1/db/migrate/20120123171827_add_verified_at_to_post.rb:9:in `up_without_benchmarks'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/relation/batches.rb:21:in `find_each'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/relation/batches.rb:21:in `each'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/relation/batches.rb:21:in `find_each'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/relation/batches.rb:71:in `find_in_batches'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/relation/batches.rb:20:in `find_each'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/base.rb:441:in `__send__'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/base.rb:441:in `find_each'
/Users/patrick/coding/rails/bounty_buy/myapp-v1/db/migrate/20120123171827_add_verified_at_to_post.rb:8:in `up_without_benchmarks'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:312:in `send'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:312:in `migrate'
/Users/patrick/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/benchmark.rb:293:in `measure'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:312:in `migrate'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:395:in `__send__'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:395:in `migrate'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:537:in `migrate'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:613:in `call'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:613:in `ddl_transaction'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:536:in `migrate'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:523:in `each'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:523:in `migrate'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:433:in `up'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:415:in `migrate'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/railties/databases.rake:142
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/Users/patrick/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/bin/rake:31
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/bin/rake:19:in `load'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/bin/rake:19
请注意,我还在保存/创建回调之前/之后评论了所有帖子模型,看看是否触发了这一点,并且它不是......
答案 0 :(得分:3)
在播放更改的列之前调用“reset_column_information”