LoginSignup
195
206

More than 1 year has passed since last update.

Rails テーブルのデータ型について

Last updated at Posted at 2019-03-03

はじめに

Railsで使用するテーブルの型の種類について、興味があったので簡単にまとめてみました。

基本コマンド

command
# マイグレーションファイル作成コマンド
$ rails generate migration クラス名

# モデル作成
$ rails generate model モデル名

クラス名は何でもOKだけど「アクション+テーブル名」とかが慣例っぽい。

モデルの新規作成はmodel モデル名。命名規則はモデル名とテーブル名の規約を参照。

「generateは g 」「destroyは d 」と略すことができるので、略して使う。

テーブルを作る、消す

作る例
$ rails g model User name:string

$ rails g model モデル名 カラム名:型
※モデル名は必ず最初を大文字にして、単数形にすること。

消す例
$ rails d model User

$ rails g model モデル名

指定できる型

  • string : 文字列
  • text : 長い文字列
  • integer : 整数
  • float : 浮動小数
  • decimal : 精度の高い小数
  • datetime : 日時
  • timestamp : タイムスタンプ
  • time : 時間
  • date : 日付
  • binary : バイナリデータ
  • boolean : Boolean

マイグレーション実行

このままだと、実際のDBには反映されないので、db:migrateコマンドを実行する。

command
# 実行
$ rails db:migrate
195
206
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
195
206