2012-06-07 50 views
1

我寫我的導軌(3.2.3)應用程序...簡單的方法來初始化耙分貝Rails應用程序:創建

的config/application.rb中:

# ... 

class MyWebApp 
    class Application < Rails::Application 

    # ... 

    config.before_initialize do 
     $my_config = MyConfig.new 
    end 
    end 
end 

的config/database.yml中:

development: 
    adapter: mysql2 
    encoding: utf8 
    reconnect: true 
    database: <%=$my_config.config['db']['basename']%>_development 
    pool: 5 
    username: <%=$my_config.config['db']['username']%> 
    password: 
    socket: /var/run/mysqld/mysqld.sock 

然後耙分貝:創建加薪:

rake aborted! 
undefined method `config' for nil:NilClass 
(erb):15:in `<main>' 
/home/sn/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/erb.rb:838:in `eval' 
/home/sn/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/erb.rb:838:in `result' 
/home/sn/dev/mywebapp/vendor/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/application/configuration.rb:115:in `database_configuration' 
... 

我手動創建數據庫後,rake db:migrate成功。所以我讀了databases.rake,發現db:migrate calls:environment task,但db:create沒有。

的ActiveRecord-3.2.3/lib目錄/ active_record/railties/databases.rake:

task :create => :load_config do 
    configs_for_environment.each { |config| create_database(config) } 
    ActiveRecord::Base.establish_connection(configs_for_environment.first) 
end 

task :migrate => [:environment, :load_config] do 
    ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true 
    ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil) do |migration| 
    ENV["SCOPE"].blank? || (ENV["SCOPE"] == migration.scope) 
    end 
end 

你知道有什麼簡單的方法來對耙分貝正確初始化$ my_config變量:創建?

回答

0

你可以依賴添加到任務,儘量把這個線在你的Rakefile的末尾:

namespace :db do 
    task :create => [:environment, :load_config] 
end 
相關問題