获取ActiveRecord :: StatementInvalid:重命名模型后找不到表

时间:2011-06-24 05:01:28

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

我以前将我的模型命名为“公司”。我没有重命名它,而是重命名了所有引用,删除了数据库并通过重新运行迁移来重新创建所有内容。我能够从SQLite资源管理器中看到该表重新创建为“公司”,但当我通过irb运行Company.new时,我得到:

ActiveRecord::StatementInvalid: Could not find table 'companies'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/sqlite_adapter.rb:295:in `table_structure'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/sqlite_adapter.rb:186:in `columns'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:680:in `columns'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/activerecord-3.0.7/lib/active_record/persistence.rb:284:in `attributes_from_column_definition'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/activerecord-3.0.7/lib/active_record/locking/optimistic.rb:62:in `attributes_from_column_definition'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:1395:in `initialize'
from (irb):1:in `new'
from (irb):1
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

我不确定“公司”的引用来自哪里 - 我在项目目录中找不到任何引用。所有的ymls等看起来都很好。

控制器:

class CompanyDirectoryController < ApplicationController
  before_filter :require_admin, :only => [:new, :create, :edit]
  def new
    @company = Company.new
    @company.companylinks.build
  end

型号:

class Company < ActiveRecord::Base
  validates_presence_of :fullname, :detailed_description, :product_info, :cat_tags, :email
  validates_uniqueness_of :fullname
  has_many :companylinks, :class_name => 'CompanyLinks',
                           :foreign_key => "pid",
                           :dependent => :destroy
  has_attached_file :company_photo, 
                    :styles => { :medium => "300x300>",
                                 :thumb => "100x100>" }
  attr_accessor :photo_file_name
  attr_accessor :photo_content_type
  attr_accessor :photo_file_size
  attr_accessor :photo_updated_at

  accepts_nested_attributes_for :companylinks,
                                :allow_destroy => true
end

路线:     resources :company, :controller => :company_directory

为什么它仍在寻找companies而不是Company?任何帮助都会很棒。

1 个答案:

答案 0 :(得分:3)

表名ActiveRecord正在寻找模型公司是公司。 Rails使用变量来复数模型的名称,以形成要在数据库中查询的表的名称。如果要重命名表或必须使用旧数据库,可以在模型中使用set_table_name来设置表名。

更新:the rails wiki还有一些其他有用的提示。