【问题标题】:Rails app confused about environment?Rails 应用程序对环境感到困惑?
【发布时间】:2011-08-05 05:52:39
【问题描述】:

我创建了一个新的 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 已存在”,因为删除只删除了myapp_development,但创建尝试创建开发和测试数据库。

【问题讨论】:

  • 我面临着完全相同的问题。我正在运行 rake db:create 和 rails 创建开发和测试模式。当我运行 rake db:drop 时,它只删除开发模式。这肯定是一个环境配置问题,因为它在一台机器(macos 雪豹)上正常工作,但在我的新机器(macos lion)上却不行。我仍在尝试找出可能导致此行为的两台机器的环境之间的差异。任何提示表示赞赏。如果我发现了什么会发布。
  • 我从来没有找到解决这个问题的方法,所以如果你能解决的话,我会很高兴听到的!

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


【解决方案1】:

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

【讨论】:

  • 对,但我没有运行rake db:create:all,我只是运行rake db:create,它正在创建测试和开发数据库。我已将我的 databases.yml 文件添加到我的问题中以防万一。
【解决方案2】:

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

另外请注意,如果数据库不存在,只需运行 rake db:migrate 就会创建数据库并迁移您的模型,您可能想改用它

【讨论】:

  • 我将我的 database.yml 文件的内容添加到问题中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 2018-10-03
  • 2012-05-21
  • 2017-07-31
  • 2014-07-08
  • 2017-02-28
  • 2013-07-04
相关资源
最近更新 更多