I wrote my rails (3.2.3) application...
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
Then, rake db:create raises:
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'
...
After I manually created database, rake db:migrate succeeds. So I read databases.rake and found that db:migrate calls :environment task but db:create does not.
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
Do you know any simple way to initialize $my_config variable correctly on rake db:create?