在RSpec spec_helper中检查挂起的迁移时如何修复ActiveRecord :: DangerousAttributeError?

时间:2013-10-30 07:57:32

标签: activerecord rspec ruby-on-rails-4 rails-migrations

好的,这是一个新的。 RSPEC。 Rails 4。

这一行在我的spec_helper.rb中:

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

当我运行一个简单的测试时(在ActiveRecord模型上):

it { should respond_to :activity }

我收到以下错误&跟踪:

/Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord 4.0.0/lib/active_record/attribute_methods.rb:59:in `instance_method_already_implemented?': version is defined by Active Record (ActiveRecord::DangerousAttributeError)
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activemodel-4.0.0/lib/active_model/attribute_methods.rb:282:in `block in define_attribute_method'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activemodel-4.0.0/lib/active_model/attribute_methods.rb:279:in `each'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activemodel-4.0.0/lib/active_model/attribute_methods.rb:279:in `define_attribute_method'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activemodel-4.0.0/lib/active_model/attribute_methods.rb:246:in `block in define_attribute_methods'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activemodel-4.0.0/lib/active_model/attribute_methods.rb:246:in `each'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activemodel-4.0.0/lib/active_model/attribute_methods.rb:246:in `define_attribute_methods'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/attribute_methods.rb:29:in `block in define_attribute_methods'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/attribute_methods.rb:26:in `synchronize'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/attribute_methods.rb:26:in `define_attribute_methods'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/attribute_methods.rb:167:in `respond_to?'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:368:in `__define_callbacks'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:79:in `run_callbacks'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/core.rb:216:in `init_with'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/persistence.rb:55:in `instantiate'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/querying.rb:45:in `block in find_by_sql'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/result.rb:21:in `block in each'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/result.rb:21:in `each'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/result.rb:21:in `each'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/querying.rb:45:in `map'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/querying.rb:45:in `find_by_sql'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/relation.rb:585:in `exec_queries'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/relation.rb:471:in `load'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/relation.rb:220:in `to_a'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:12:in `map'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/migration.rb:787:in `get_all_versions'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/migration.rb:793:in `current_version'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/migration.rb:800:in `needs_migration?'
    from /Users/dev/.rvm/gems/ruby-2.0.0-p247@projectname/gems/activerecord-4.0.0/lib/active_record/migration.rb:379:in `check_pending!'
    from /projects/subset_of_projects/this_project_group/projectname/spec/spec_helper.rb:17:in `<top (required)>'

1 个答案:

答案 0 :(得分:0)

我添加了paper_trail gem但没有运行安装/迁移。真的很简单明了。但这不是我以前见过的错误,所以我会发布这个,所以其他可能遇到它的人可以在浪费很长时间之前检查明显的事情。