Rails app对环境感到困惑?

时间:2011-04-14 04:44:14

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

我创建了一个新的Ruby on Rails应用程序(我在Rails 3中正在开发的第一个应用程序),出于某种原因,我的应用程序似乎对它应该运行的环境感到困惑(除非我只是困惑)。

当我运行rake db:create时,它会同时创建“myapp_development”和“myapp_test”数据库。当我随后运行rake db:drop时,它会删除开发数据库,​​但不会删除测试数据库。

发生了什么事?

编辑1:以下是我的database.yml文件的样子:

development:
  adapter: mysql
  database: myapp_development
  username: root
  password: 

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql
  database: myapp_test
  username: root
  password: 

production:
  adapter: mysql
  database: myapp_production
  username: root
  password: 

编辑2 :尝试从头开始重新创建新应用,但问题仍然存在。这就是我所做的。

1.创建了一个新的rails应用程序:

rails new myapp

2.编辑我的Gemfile:

source 'http://rubygems.org'

gem 'rails', '3.0.6'
gem 'mysql', '2.8.1'

# Bundle the extra gems:
gem 'warden', '1.0.3'
gem 'devise', '1.2.1'
gem 'geokit'

# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
group :development, :test do
  gem 'ruby-debug'
end

3.编辑我的database.yml:

development:
  adapter: mysql
  database: myapp_development
  username: root
  password: 
  host: localhost

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql
  database: myapp_test
  username: root
  password: 
  host: localhost

production:
  adapter: mysql
  database: myapp_production
  username: root
  password: 
  host: localhost

4.此时运行rake db:create会同时创建myapp_developmentmyapp_test。如果我继续运行rake db:drop后跟rake db:create,我会收到一条命令行警告,指出“myapp_test已经存在”,因为drop只会删除myapp_development,但是create会尝试创建dev和测试数据库。

2 个答案:

答案 0 :(得分:1)

如果您运行rake db:create:all而不是rake db:create,它只会创建开发和测试数据库。后者只会创建一个或另一个,具体取决于您正在使用的环境。

答案 1 :(得分:0)

检查你的 config/database.yml 在您的应用程序中为数据库配置提供文件,您可能需要进行测试。但是database.yml会让你访问一切。默认情况下,它将使用开发db,但您指定何时使用生产

另外注意,只运行rake db:migrate会创建数据库(如果它们不存在)并迁移模型,您可能希望使用它而不是