Railsでmodelを作成（SQLite）


# Railsでmodelを作成（SQLite）

## 1. 手順

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

```bash:
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を確認

```bash:
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
```

```bash:
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
```

```diff:
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開発〜](http://qiita.com/fk_2000/items/28a1564150eabfd6468b)