2

我正在学习 rails,当我开始并安装基于我正在阅读的书籍的 sqlite3 的所有内容时。但是,我正在尝试部署我在 heroku 上编写的测试应用程序。Heroku 建议应用程序运行 postgresql 进行开发和生产,以防止出现细微的错误。所以我决定从今以后对我所有的 Rails 应用程序使用 post gresql。

为此,我进行了以下设置:
1)下载并安装了 Postgresql
2)在我的 .bash_rc 文件中添加了文件夹的路径
3)安装了 pg gem
4)到目前为止一切都很好。但是,当我转到我的应用程序的根文件夹并尝试以下命令时:

rails generate scaffold User name:string email:string

我在控制台上收到以下错误

rails generate scaffold User name:string email:string

invoke  active_record 
/Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/bundler-
1.0.22/lib/bundler/rubygems_integration.rb:143:in `block in replace_gem': 
Please install the sqlite3 adapter: `gem install activerecord-sqlite3-adapter` 
(sqlite3   is not part of the bundle. Add it to Gemfile.) (LoadError)
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/activerecord- 
3.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:3:in `<top (required)>'

这告诉我,我的 rails 应用程序中的默认设置清楚地适用于 sqlite3。所以我打开了我的 databse.yml 文件,这就是它的内容。

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# 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: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

一切都为 sqlite3 设置了如何更改我的 rails 设置,以便默认情况下数据库的所有配置都用于 postgresql?

谢谢

4

2 回答 2

6

更新:查看 - http://railscasts.com/episodes/342-migrating-to-postgresql

您只需要更改 database.yml 文件中的设置,以便它知道您要使用什么。在您的情况下,您需要更改适配器和数据库:

development:
  adapter: postgresql
  database: my_app_dev
  username: username
  password: pw

一旦你把它放在那里,你可以运行以下命令来让它与你在 sqlite 中的内容相匹配:

rake db:create && rake db:schema:load
于 2012-04-25T23:07:12.450 回答
3

自从您提出问题以来,不确定这是否是一项新功能,但是将“-d postgresql”传递给rails new命令,它将使用 postgres 信息填充 config/database.yml。

rails new testapp -d postgresql
于 2013-05-23T19:53:44.543 回答