2013-08-07 60 views
0

我安裝了prostgres以與我的rails4應用程序一起使用,並且我能夠連接到數據庫服務器並創建數據庫。然後我對Gemfile和config/database.yml進行了必要的更改以使用postgres。然後,我創建了一個新的應用程序,並在應用程序,我創建了一個用戶模型:如何讓我的rails應用程序使用我的postgresql數據庫?

$ rails generate model User name:string email:string 
     invoke active_record 
     create db/migrate/20130806145935_create_users.rb 
     create app/models/user.rb 
     invoke rspec 
     create  spec/models/user_spec.rb 

然後我做:

$ bundle exec rake db:migrate 
== CreateUsers: migrating ==================================================== 
-- create_table(:users) 
    -> 0.1498s 
== CreateUsers: migrated (0.1500s) =========================================== 

但在db目錄,我看到這些文件:

development.sqlite3 
test.sqlite3 

此外,當我嘗試使用SQLite數據庫瀏覽器查看這些文件時,出現錯誤,表示它們不是sqlite 3數據庫。事實上,它們是空的:

~/rails_projects/sample_app4_0/db$ ls -al 
total 40 
drwxr-xr-x 8 7stud staff 272 Aug 6 22:50 . 
drwxr-xr-x 24 7stud staff 816 Aug 6 22:23 .. 
-rw-r--r-- 1 7stud staff  0 Jul 30 00:21 development.sqlite3 
drwxr-xr-x 3 7stud staff 102 Aug 6 22:22 migrate 
-rw-r--r-- 1 7stud staff 1063 Aug 6 09:06 schema.rb 
-rw-r--r-- 1 7stud staff 343 Jul 29 20:01 seeds.rb 
-rw-r--r-- 1 7stud staff  0 Jul 30 03:02 test.sqlite3 

我讀了你可以告訴它使用Postgres的一個標誌,應用程序創建的軌道:

rails new myapp --database postgresql 

,但我已經創建了我的應用程序。我必須重新開始嗎?如果這有所作爲,我正在使用git。

我的Gemfile:

source 'https://rubygems.org' 
ruby '2.0.0' 
#ruby-gemset=railstutorial_rails_4_0 

gem 'rails', '4.0.0' 
gem 'bootstrap-sass', '2.3.2.0' 
#Added for postgres: 
gem 'pg', '0.15.1' 


group :development, :test do 
    #gem 'sqlite3', '1.3.7' 
    gem 'rspec-rails', '2.13.1' 
    gem 'guard-rspec', '2.5.0' 

    gem 'spork-rails', github: 'sporkrb/spork-rails' 
    gem 'guard-spork', '1.5.0' 
    gem 'childprocess', '0.3.6' 
end 

group :test do 
    gem 'selenium-webdriver', '2.0.0' 
    gem 'capybara', '2.1.0' 
    gem 'growl', '1.0.3' 
end 

gem 'sass-rails', '4.0.0' 
gem 'uglifier', '2.1.1' 
gem 'coffee-rails', '4.0.0' 
gem 'jquery-rails', '2.2.1' 
gem 'turbolinks', '1.1.1' 
gem 'jbuilder', '1.0.2' 

group :doc do 
    gem 'sdoc', '0.3.20', require: false 
end 

group :production do 
    #gem 'pg', '0.15.1' 
    gem 'rails_12factor', '0.0.2' 
end 

的config/database.yml中:

development: 
    adapter: postgresql 
    encoding: utf8 
    database: sampleapp_dev #can be anything unique 

    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: postgresql 
    encoding: utf8 
    database: sampleapp_test #can be anything unique 

    pool: 5 
    timeout: 5000 

production: 
    adapter: postgresql 
    database: sampleapp_prod 

    pool: 5 
    timeout: 5000 

回答

13

好了,要弄清楚這是怎麼回事我使用--database標誌創建一個新的測試程序:

rails_projects$ rails new test_postgres --database postgresql 

事實證明,所做的只是設置你的config/database.yml文件,如下所示:

development: 
    adapter: postgresql 
    encoding: unicode 
    database: test_postgres_development 
    pool: 5 
    username: test_postgres 
    password: 

test: 
    adapter: postgresql 
    encoding: unicode 
    database: test_postgres_test 
    pool: 5 
    username: test_postgres 
    password: 

production: 
    adapter: postgresql 
    encoding: unicode 
    database: test_postgres_production 
    pool: 5 
    username: test_postgres 
    password: 

和空sqlite的文件:

development.sqlite3 
test.sqlite3 

仍然存在於數據庫目錄中。

然後我啓動了postgres,我用pgAdmin3連接到postgres數據庫服務器。 (如果這是混亂的,請參閱:How do I start enterpiseDB PostgreSQL on Mac OSX 10.6.8?

然後,我創建了一個模型:

~/rails_projects$ cd test_postgres/ 
~/rails_projects/test_postgres$ rails g scaffold Post title:string author:string body:text 

     invoke active_record 
     create db/migrate/20130807061320_create_posts.rb 
     create app/models/post.rb 
     invoke test_unit 
     create  test/models/post_test.rb 
     create  test/fixtures/posts.yml 
     invoke resource_route 
     route resources :posts 
     invoke scaffold_controller 
     create app/controllers/posts_controller.rb 
     invoke erb 
     create  app/views/posts 
     create  app/views/posts/index.html.erb 
     create  app/views/posts/edit.html.erb 
     create  app/views/posts/show.html.erb 
     create  app/views/posts/new.html.erb 
     create  app/views/posts/_form.html.erb 
     invoke test_unit 
     create  test/controllers/posts_controller_test.rb 
     invoke helper 
     create  app/helpers/posts_helper.rb 
     invoke  test_unit 
     create  test/helpers/posts_helper_test.rb 
     invoke jbuilder 
     create  app/views/posts/index.json.jbuilder 
     create  app/views/posts/show.json.jbuilder 
     invoke assets 
     invoke coffee 
     create  app/assets/javascripts/posts.js.coffee 
     invoke scss 
     create  app/assets/stylesheets/posts.css.scss 
     invoke scss 
     create app/assets/stylesheets/scaffolds.css.scss 

然後我試圖執行遷移:

~/rails_projects/test_postgres$ bundle exec rake db:migrate 
rake aborted! 
FATAL: role "test_postgres" does not exist 
... 
... 

那出錯,因爲我不有一個數據庫用戶(或角色在postgres說)名爲test_postgres - 它可能沒有其他人會。當我設置postgres時,我創建了用戶7stud,這是我的Mac用戶名。爲了解決這個rails錯誤,我只是在config/database.yml的三行上將用戶名改成了7stud。其實,我甚至不需要那樣做。因爲我在我的Mac用戶7stud,這意味着我執行我的導軌作爲7stud命令,並加上事實證明我的Postgres數據庫是建立一個名爲7stud用戶,可以讓我完全消除配置用戶名線/ database.yml的:

development: 
    adapter: postgresql 
    encoding: unicode 
    database: test_postgres_development 
    pool: 5 

test: 
    adapter: postgresql 
    encoding: unicode 
    database: test_postgres_test 
    pool: 5 

production: 
    adapter: postgresql 
    encoding: unicode 
    database: test_postgres_production 
    pool: 5 

然後我試圖再次執行遷移:

~/rails_projects/test_postgres$ bundle exec rake db:migrate 
rake aborted! 
FATAL: database "test_postgres_development" does not exist 
... 
... 

注意,database.yml文件,指定了三個數據庫的名稱。所以,我創建了開發和測試數據庫:

~$ cd /Library/PostgreSQL/9.2/ 
/Library/PostgreSQL/9.2$ sudo su postgres 
Password: <normal sudo password> 
bash-3.2$ createdb -O7stud -Eutf8 test_postgres_development 
bash-3.2$ createdb -O7stud -Eutf8 test_postgres_test 

-O所有者
-E編碼

然後我試圖再次執行遷移:

~/rails_projects/test_postgres$ bundle exec rake db:migrate 
== CreatePosts: migrating ==================================================== 
-- create_table(:posts) 
    -> 0.0441s 
== CreatePosts: migrated (0.0443s) =========================================== 

成功(但在網絡上其他地方示例通過'Notice'行顯示不同的輸出)。

然後,我開始一個服務器:

~/rails_projects/test_postgres$ rails s 

...在我的瀏覽器我進入的網址:

http://localhost:3000/posts 

...我創建了幾個帖子。

然後使用pgAdmin3,我試圖找到在test_postgres_development數據庫中的職位。首先,我在刷新的pgAdmin3「數據庫(3)」行通過右鍵點擊就行了,然後選擇「刷新」。然後我搜索了很長時間的test_postgres_development目錄,沒有運氣。根據架構,我看得出來,載有崗位目錄,所以表得到了創建數據表目錄,但我無法弄清楚如何,看看是否有表中的任何條目。事實證明,你必須右鍵點擊帖子目錄,然後選擇'查看數據',瞧那裏有我創建的帖子。

也可以查看使用命令行中的條目:

~$ cd /Library/PostgreSQL/9.2/ 
/Library/PostgreSQL/9.2$ sudo su postgres 
Password: <normal sudo password> 
bash-3.2$ psql -U 7stud test_postgres_development 
psql (9.2.4) 
Type "help" for help. 

test_postgres_development=> \dt 

      List of relations 
Schema |  Name  | Type | Owner 
--------+-------------------+-------+------- 
public | posts    | table | 7stud 
public | schema_migrations | table | 7stud 
(2 rows) 

test_postgres_development=> \d posts 

            Table "public.posts" 
    Column |   Type    |      Modifiers      
------------+-----------------------------+---------------------------------------------------- 
id   | integer      | not null default nextval('posts_id_seq'::regclass) 
title  | character varying(255)  | 
author  | character varying(255)  | 
body  | text      | 
created_at | timestamp without time zone | 
updated_at | timestamp without time zone | 
Indexes: 
    "posts_pkey" PRIMARY KEY, btree (id) 


test_postgres_development=> select id, title, author, created_at from posts; 


id |   title   | author |   created_at   
----+------------------------+--------------+---------------------------- 
    1 | Hi there.    | Tom   | 2013-08-07 06:24:57.806237 
    2 | Ola!     | Nancy  | 2013-08-07 06:25:23.989643 
(2 rows) 

test_postgres_development=> 
+0

感謝您的回答。很好的解釋。 –

相關問題