2016-02-23 12 views

回答

1

是有可能,這裏是如何:你

  1. 確保在config/database.yml

  2. 運行rake db:schema:dump

提示就在旁邊正確配置數據庫連接:運行rake -T db:schema並且您將獲得與數據庫模式任務相關的所有可用命令的列表:

rake db:schema:cache:clear # Clear a db/schema_cache.dump file 
rake db:schema:cache:dump # Create a db/schema_cache.dump file 
rake db:schema:dump   # Create a db/schema.rb file that is portable against any DB supported by AR 
rake db:schema:load   # Load a schema.rb file into the database 

但是生成模型是一個不同的故事,特別是如果您現有的數據庫不遵循軌道約定的命名。

有一些像rmre這樣的寶石是專門用於這個,但它似乎沒有維護。

如果你的遺留數據庫不是一個怪物,你可以很快自行創建的模型 - 這裏的竅門是不會產生任何遷移,即:

rails g model YourModel --migration=false

然後,你必須去適應像一些關鍵屬性表格名稱和ActiveRecord命名約定的主鍵,可以按如下方式完成:

class YourModel < ActiveRecord::Base 
    self.table_name = 'a_legacy_table_name' 
    self.primary_key = 'primary_key_column_name' 

    belongs_to :other_model, 
    foreign_key: 'foreign_key_on_other_table' 

    has_many :other_models, 
    foreign_key: 'foreign_key_in_this_table', 
    primary_key: 'primary_key_on_other_table' 
end 
相關問題