LoginSignup
1

More than 5 years have passed since last update.

【Ruby】DB概要

Last updated at Posted at 2018-12-16

DB(データベース)とは

  • データを保存しておく場所 のこと。

DBの構成

  • テーブル:表で管理しているデータ(下図全体)
  • カラム:縦の列(下図赤いセル)
  • レコード:1行ずつのデータ(下図オレンジのセル) スクリーンショット 2018-12-16 23.09.30.png

DBの作成方法

1.DBに変更を指示するファイルを作る

$ rails g(generate) model Post content:text
             モデル名 カラム名 データ型

2.DBに変更を反映する

$ rails db:migrate

3.インスタンス作成

  • 空のインスタンスを作成する場合
$ post = Post.new #空のインスタンスを作成
$ post.content = "こんにちは" #contentカラムに内容(こんにちは)を追加
$ post.save #DBに保存
  • カラムの中身を伴い、インスタンスを登録する場合
$ post = Post.new(content: "こんにちは")
  # $インスタンス作成時点でcontentカラムを作成しているため、
  post.content とコマンドを打つと"こんにちは"が表示される
$ post.save

結果、DBの値は下記のように登録される。
68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f3330393639312f37393932393661662d646538612d626239312d363263622d3438316138663630323964322e706e67.png

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