rake test - self.table_name无法识别

时间:2013-07-24 20:30:31

标签: ruby-on-rails database ruby-on-rails-3 rake

我有一个奇怪的错误。当我尝试运行单元测试时,它会给我以下错误:

test_the_truth(UpdateMainTest):
 ActiveRecord::StatementInvalid: Could not find table 'blacklists'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:472:in `table_structure'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:346:in `columns'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:660:in `column_names'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:652:in `timestamp_column_names'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:585:in `block in table_rows'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:579:in `each'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:579:in `map'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:579:in `table_rows'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:494:in `block (3 levels) in create_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:492:in `each'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:492:in `block (2 levels) in create_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:491:in `block in create_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:168:in `disable_referential_integrity'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:476:in `create_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:895:in `load_fixtures'
/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:849:in `setup_fixtures'
/var/lib/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:407:in `_run__379848442555604621__setup__3487204981098397389__callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
/var/lib/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/testing/setup_and_teardown.rb:35:in `run'

这是我的黑名单模型:

class Blacklist < ActiveRecord::Base
  attr_accessible :abbreviation, :name
  self.table_name = "blacklist"
  validates :abbreviation, :presence => :true, :uniqueness => :true
  validates :name, :presence => :true
end

架构还正确列出了表格:

  create_table "blacklist", :force => true do |t|
    t.string   "name"
    t.string   "abbreviation"
    t.datetime "created_at",   :null => false
    t.datetime "updated_at",   :null => false
  end

我跑了rake db:test:准备了不止一次,但错误仍然存​​在。 当我使用rails控制台时,我可以创建黑名单对象而不会出错。 知道我能做什么吗?

3 个答案:

答案 0 :(得分:0)

使用复数形式的符号,表名应为复数:

self.table_name = :blacklists

使用复数形式创建表名:

create_table "blacklists", :force => true do |t|
    t.string   "name"
    t.string   "abbreviation"
    t.datetime "created_at",   :null => false
    t.datetime "updated_at",   :null => false
  end

答案 1 :(得分:0)

如果仅在您的单元测试中发生,则测试数据库可能已过时。您应该尝试重建它rake db:migrate db:test:clone

答案 2 :(得分:0)

感谢您的帮助。

我无法真正追查问题的根源,但是当我 注释掉test_helper.rb中加载所有灯具的行,错误没有再次出现。