LoginSignup
1
2

More than 5 years have passed since last update.

DataBase作成メモ:Ruby on Rails v5.0.1(環境macOS Sierra v10.12.3)

Last updated at Posted at 2017-02-18

Qiita初登録&初投稿です。これまでは閲覧側で、多くの方の有用な記事に助けられて来ました。
ヒヨコ🐥に毛が生えた程度のビギナーですが宜しくお願い致します(_ _)。
と言う事で、試しに1つ掲載します。

rails newコマンドで新規作成すると、dbディレクトリとシードデータと言うデータを保存する為のseeds.rbファイルが作成される。

create  db
create  db/seeds.rb

この後でlocalhost:3000にアクセスすることで、自動的にdevelopment.sqlite3と言うデータベースが作成される。因みに、このデータベースは削除しても上記URLにアクセスする度に生き返る。
$ ls
development.sqlite3 seeds.rb

データベース作成のコマンド。既にデータベースdevelopment.sqlite3は存在していると表示されるが、新たにデータベースtest.sqlite3が作成される。
$ rake db:create
Database 'db/development.sqlite3' already exists
Created database 'db/test.sqlite3'

Railsでは3つのデータベース:development.sqlite3(開発用)、test.sqlite3(テスト用)、production.sqlite3(本番用)を使用出来る。

$ rake db:create RAILS_ENV=production
Created database 'db/production.sqlite3'

development.sqlite3、test.sqlite3のデータベースは下記コマンドで削除出来るが、production.sqlite3は削除出来ない。
$ rake db:drop

$ rake db:drop RAILS_ENV=production

DBMSへの接続設定はconfig/database.ymlファイル(YAML形式)で行い、アプリケーション作成時に自動生成される。

DBMSを指定しないでrails newすると、デフォルトでSQLite3用の設定を作成する。Railsには3つの実行環境(開発、テスト、本番)があり、環境ごとに別々のデータベースを使うことになる。
# SQLite version 3.x

gem install sqlite3

Ensure the SQLite 3 gem is defined in your Gemfile

gem 'sqlite3'

default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

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:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

また、実行環境ごとの設定ファイルはconfig/environmentsに、development.rb、test.rb、production.rbがある。
1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2