2011-06-24 77 views
2

我之前將我的模型命名爲「Companies」,這不正確。我重命名所有引用,刪除數據庫並重新運行遷移,重新創建所有內容,而不是重命名它。我能看到SQLite的探險家重新「公司」表,但是當我通過IRB運行Company.new,我得到:獲取ActiveRecord :: StatementInvalid:重命名模型後無法找到表格

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?任何幫助都會很棒。

回答

3

表名ActiveRecord正在尋找模型公司是公司。 Rails使用變換來複制模型的名稱以形成要在數據庫中查詢的表的名稱。如果要重命名錶或必須使用舊數據庫,則可以在模型中使用set_table_name來設置表名。

更新:還有一些其他有用的提示在the rails wiki

+0

謝謝。我在想,表名應該像模型一樣單數。 –

+0

如果您可以從Pragmatic Programmers獲得Rails的敏捷Web開發副本,那麼掌握關鍵軌概念以及一些優秀的軟件設計原則將會是一次不錯的閱讀。 – Chandranshu

相關問題