1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsでmodelを作成(SQLite)

Last updated at Posted at 2016-08-31

Railsでmodelを作成(SQLite)

1. 手順

1.1. rails generate model xxxx コマンドでモデルを作成

user01:~/workspace/demo $ rails generate model user
Running via Spring preloader in process 21331
      invoke  active_record
      create    db/migrate/20160830073718_create_users.rb
      create    app/models/user.rb
user01:~/workspace/demo $

1.2. 作成されたマイグレーションファイルとmodelを確認

user01:~/workspace/demo $ cat db/migrate/20160830073718_create_users.rb
class CreateUsers < ActiveRecord::Migration[5.0]
  def change
    create_table :users do |t|

      t.timestamps
    end
  end
end
user01:~/workspace/demo $ cat app/models/user.rb
class User < ApplicationRecord
end
user01:~/workspace/demo $

1.3. マイグレーションファイルを編集

user01:~/workspace/demo $ vi db/migrate/20160830073718_create_users.rb
class CreateUsers < ActiveRecord::Migration[5.0]
  def change
    create_table :users do |t|
+      t.string :name
+      t.integer :gender
+      t.date :birthday
+      t.string :hometown
+      t.text :remarks
      t.timestamps
    end
  end
end

1.4. データベースをdatabase.ymlで確認

vi config/database.yml
# 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

データベース名は「db/development.sqlite3」

1.5. DBにテーブルが無いことを確認

user01:~/workspace/demo $ sqlite3 db/development.sqlite3
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from sqlite_master;
sqlite> .exit
user01:~/workspace/demo $ 

1.6. DBとテーブルを作成

user01:~/workspace/demo $ rake db:create
Database 'db/development.sqlite3' already exists
Created database 'db/test.sqlite3'
user01:~/workspace/demo $ rake db:migrate
== 20160830073718 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0030s
== 20160830073718 CreateUsers: migrated (0.0033s) =============================

user01:~/workspace/demo $

1.7. DBに接続し、SQLでテーブルを確認

user01:~/workspace/demo $ sqlite3 db/development.sqlite3
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
sqlite> select * from sqlite_master;
table|schema_migrations|schema_migrations|2|CREATE TABLE "schema_migrations" ("version" varchar NOT NULL PRIMARY KEY)
index|sqlite_autoindex_schema_migrations_1|schema_migrations|3|
table|ar_internal_metadata|ar_internal_metadata|4|CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
index|sqlite_autoindex_ar_internal_metadata_1|ar_internal_metadata|5|
table|users|users|6|CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "gender" integer, "birthday" date, "hometown" varchar, "remarks" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
table|sqlite_sequence|sqlite_sequence|7|CREATE TABLE sqlite_sequence(name,seq)
sqlite> 

1.8. DB接続を切断

sqlite> .exit
user01:~/workspace/demo $ 

前回の記事

Rails初心者〜構築からMVC開発〜

1
3
0

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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?