Ruby on Rails: DBにテーブルを作る〜テーブルにカラムを作成する...chapter2
DB設計は終わりました。
今回やりたいこと
§ DBにUsersテーブルとTweetsテーブルの作成
§ 作ったテーブルに各カラムを作成
①ModelにUserモデルとTweetモデルを作成
モデルはDBに値をとりに行ったり入れたりするために必要なもの。
$ rails g model User
すると、
invoke active_record
create db/migrate/20200526224538_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
となる。
後で、db/migrate/20200526224538_create_users.rbにカラムや必要情報を追加していきます。
同様にTweetもモデルを作成します。
結果:
invoke active_record
create db/migrate/20200526225124_create_tweets.rb
create app/models/tweet.rb
invoke test_unit
create test/models/tweet_test.rb
create test/fixtures/tweets.yml
上同様、db/migrate/20200526225124_create_tweets.rbにカラム、必要情報を追加します。
②migrateフォルダ内に作成されたファイルの変更
db/migrate/20200526224538_create_users.rb
db/migrate/20200526225124_create_tweets.rb
と変更した。
③$ rails g db:migrate △
調べました。
なぜなら、うろ覚えの私は$rails g ~~migrationってコマンドも覚えていたからです(~~は何か書いてあった気がする....(うろ覚え))。
1.$rails g migration create_△△
これで先ほど作ったdb/migrate/○○_create_△△.rbが作られる(○○は数字が入る)。
つまりこれがマイグレーションファイルっていうものだったんですね。
でも、マイグレーションファイルって結局なんだろってなりました。
DBにカラムを作るものって認識でした。
🔍調べた結果:
マイグレーションファイルとは、Rubyで書かれた テーブルの設計図 のことです。 テーブルの設計図とは カラム や データ型 の定義が書かれた状態のことを指します。
参照:https://diveintocode.jp/blogs/Technology/migration
確かに書いてる。
2.$rails db:migrate
1.は何してるか分かった。
じゃあ、2.って何をしてるの?どんな指令を出してるの???
🔍調べた結果:
マイグレーションファイルを実行し、データベースに反映させるためのコマンドです。
参照:https://pikawaka.com/rails/migration
知らなかった。
これをしないとDBにカラムが追加されない。その前にテーブルもない状態。
というわけで、マイグレーションファイルは変更したので$rails db:migrateをします。
==20200526224538 CreateUsers: migrating ======================================
-- create_table(:users)
-> 0.0395s
==20200526224538 CreateUsers: migrated (0.0396s) =============================
==20200526225124 CreateTweets: migrating =====================================
-- create_table(:tweets)
-> 0.0553s
==20200526225124 CreateTweets: migrated (0.0554s) ============================
これでSequel Proを見にいくと出来てるぞ!
ちなみにコマンドでマイグレーションファイルがschema_migrationsテーブルに保存できているかを確認する方法。
$rails db:migrate:status
実行結果:
と、upになっていれば保存できてます。
downとなっている場合は保存できてません。
以上です。
Ruby on Rails: README.mdにDB設計を書く...chapter1-2
DBで管理したいテーブル、そのテーブル中のカラム、モデル同士の関係性(アソシエーション)を考える
1.必要なテーブルを考える
→UsersテーブルとTweetsテーブルを作る。
ツイートを投稿するので Tweetsテーブルを作成。
その後、ユーザーと紐付けて管理したいのでUsersテーブルを作成。
( Tweetsテーブルと複数形にした理由としてはツイートしたものを複数管理するため、Usersテーブルも同様にユーザーを個々で管理したいため複数形にした。)←これ、新規登録/ログイン機能必要になってくるはず(今気がついた。後でmigrateでカラムとかをDBに作るのですが、gemでdeviseで作り替えるべき???)
2.テーブルで管理したいカラムと型を考える
→Userテーブルで管理したいカラムはニックネームなのでnickname。
nicknameは文字列なのでstring型にしました。
→Tweetテーブルで管理したいカラムはタイトル(title)、本文(main)。最終的にはどのユーザーのものかを紐づけたいのでuser_idもカラムに追加しました。
titleは長文でないのでstring型。
mainは本文なので長文になるのでtext型。
user_idは整数なのでinteger型としました。
ちなみに型は↓があるみたい。
- string : 文字列
- text : 長い文字列
- integer : 整数
- float : 浮動小数
- decimal : 精度の高い小数
- datetime : 日時
- timestamp : タイムスタンプ
- time : 時間
- date : 日付
- binary : バイナリデータ
- boolean : Boolean
3.テーブル同士の関係性(アソシエーション)を考える
UsersテーブルとTweetsテーブルの関係性は、一人のユーザーは複数のツイートをできる。しかしユーザーはツイートしない(0)でもおkです。
↓こんな感じ?
%
5.README.mdに記載
Optionsに記載したもの。
Tweetsテーブルに書いてるforeign_key: trueこれで外部のUsersテーブルからユーザー一人一人に割り当てられるidを拾ってこられる。
またニックネーム、タイトル、本文、ユーザーのidを空の状態で保存できるようにしたくないのでnull: falseとしました。
以上です。 %3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20value%3D%22Users%22%20style%3D%22swimlane%3BfontStyle%3D2%3Balign%3Dcenter%3BverticalAlign%3Dtop%3BchildLayout%3DstackLayout%3Bhorizontal%3D1%3BstartSize%3D26%3BhorizontalStack%3D0%3BresizeParent%3D1%3BresizeLast%3D0%3Bcollapsible%3D1%3BmarginBottom%3D0%3Brounded%3D0%3Bshadow%3D0%3BstrokeWidth%3D1%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22220%22%20y%3D%22120%22%20width%3D%22160%22%20height%3D%2252%22%20as%3D%22geometry%22%3E%3CmxRectangle%20x%3D%22230%22%20y%3D%22140%22%20width%3D%22160%22%20height%3D%2226%22%20as%3D%22alternateBounds%22%2F%3E%3C%2FmxGeometry%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%223%22%20value%3D%22nickname%22%20style%3D%22text%3Balign%3Dleft%3BverticalAlign%3Dtop%3BspacingLeft%3D4%3BspacingRight%3D4%3Boverflow%3Dhidden%3Brotatable%3D0%3Bpoints%3D%5B%5B0%2C0.5%5D%2C%5B1%2C0.5%5D%5D%3BportConstraint%3Deastwest%3B%22%20vertex%3D%221%22%20parent%3D%222%22%3E%3CmxGeometry%20y%3D%2226%22%20width%3D%22160%22%20height%3D%2226%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%224%22%20value%3D%22Tweet%22%20style%3D%22swimlane%3BfontStyle%3D0%3Balign%3Dcenter%3BverticalAlign%3Dtop%3BchildLayout%3DstackLayout%3Bhorizontal%3D1%3BstartSize%3D26%3BhorizontalStack%3D0%3BresizeParent%3D1%3BresizeLast%3D0%3Bcollapsible%3D1%3BmarginBottom%3D0%3Brounded%3D0%3Bshadow%3D0%3BstrokeWidth%3D1%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22508%22%20y%3D%22120%22%20width%3D%22160%22%20height%3D%22110%22%20as%3D%22geometry%22%3E%3CmxRectangle%20x%3D%22550%22%20y%3D%22140%22%20width%3D%22160%22%20height%3D%2226%22%20as%3D%22alternateBounds%22%2F%3E%3C%2FmxGeometry%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%225%22%20value%3D%22User_id%22%20style%3D%22text%3Balign%3Dleft%3BverticalAlign%3Dtop%3BspacingLeft%3D4%3BspacingRight%3D4%3Boverflow%3Dhidden%3Brotatable%3D0%3Bpoints%3D%5B%5B0%2C0.5%5D%2C%5B1%2C0.5%5D%5D%3BportConstraint%3Deastwest%3B%22%20vertex%3D%221%22%20parent%3D%224%22%3E%3CmxGeometry%20y%3D%2226%22%20width%3D%22160%22%20height%3D%2226%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%226%22%20value%3D%22title%22%20style%3D%22text%3Balign%3Dleft%3BverticalAlign%3Dtop%3BspacingLeft%3D4%3BspacingRight%3D4%3Boverflow%3Dhidden%3Brotatable%3D0%3Bpoints%3D%5B%5B0%2C0.5%5D%2C%5B1%2C0.5%5D%5D%3BportConstraint%3Deastwest%3Brounded%3D0%3Bshadow%3D0%3Bhtml%3D0%3B%22%20vertex%3D%221%22%20parent%3D%224%22%3E%3CmxGeometry%20y%3D%2252%22%20width%3D%22160%22%20height%3D%2226%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%227%22%20value%3D%22main%22%20style%3D%22text%3Bhtml%3D1%3Balign%3Dleft%3BverticalAlign%3Dmiddle%3Bresizable%3D0%3Bpoints%3D%5B%5D%3Bautosize%3D1%3B%22%20vertex%3D%221%22%20parent%3D%224%22%3E%3CmxGeometry%20y%3D%2278%22%20width%3D%22160%22%20height%3D%2220%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%228%22%20value%3D%22%22%20style%3D%22endArrow%3Dopen%3Bshadow%3D0%3BstrokeWidth%3D1%3BstrokeColor%3D%23000000%3Brounded%3D0%3BendFill%3D1%3BedgeStyle%3DelbowEdgeStyle%3Belbow%3Dvertical%3B%22%20edge%3D%221%22%20source%3D%222%22%20target%3D%224%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%220.5%22%20y%3D%2241%22%20relative%3D%221%22%20as%3D%22geometry%22%3E%3CmxPoint%20x%3D%22380%22%20y%3D%22192%22%20as%3D%22sourcePoint%22%2F%3E%3CmxPoint%20x%3D%22540%22%20y%3D%22192%22%20as%3D%22targetPoint%22%2F%3E%3CmxPoint%20x%3D%22-40%22%20y%3D%2232%22%20as%3D%22offset%22%2F%3E%3C%2FmxGeometry%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%229%22%20value%3D%220..1%22%20style%3D%22resizable%3D0%3Balign%3Dleft%3BverticalAlign%3Dbottom%3BlabelBackgroundColor%3Dnone%3BfontSize%3D12%3B%22%20connectable%3D%220%22%20vertex%3D%221%22%20parent%3D%228%22%3E%3CmxGeometry%20x%3D%22-1%22%20relative%3D%221%22%20as%3D%22geometry%22%3E%3CmxPoint%20y%3D%224%22%20as%3D%22offset%22%2F%3E%3C%2FmxGeometry%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%2210%22%20value%3D%22N%22%20style%3D%22resizable%3D0%3Balign%3Dright%3BverticalAlign%3Dbottom%3BlabelBackgroundColor%3Dnone%3BfontSize%3D12%3B%22%20connectable%3D%220%22%20vertex%3D%221%22%20parent%3D%228%22%3E%3CmxGeometry%20x%3D%221%22%20relative%3D%221%22%20as%3D%22geometry%22%3E%3CmxPoint%20x%3D%22-7%22%20y%3D%224%22%20as%3D%22offset%22%2F%3E%3C%2FmxGeometry%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphModel%
%3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20value%3D%22Users%22%20style%3D%22swimlane%3BfontStyle%3D2%3Balign%3Dcenter%3BverticalAlign%3Dtop%3BchildLayout%3DstackLayout%3Bhorizontal%3D1%3BstartSize%3D26%3BhorizontalStack%3D0%3BresizeParent%3D1%3BresizeLast%3D0%3Bcollapsible%3D1%3BmarginBottom%3D0%3Brounded%3D0%3Bshadow%3D0%3BstrokeWidth%3D1%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22220%22%20y%3D%22120%22%20width%3D%22160%22%20height%3D%2252%22%20as%3D%22geometry%22%3E%3CmxRectangle%20x%3D%22230%22%20y%3D%22140%22%20width%3D%22160%22%20height%3D%2226%22%20as%3D%22alternateBounds%22%2F%3E%3C%2FmxGeometry%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%223%22%20value%3D%22nickname%22%20style%3D%22text%3Balign%3Dleft%3BverticalAlign%3Dtop%3BspacingLeft%3D4%3BspacingRight%3D4%3Boverflow%3Dhidden%3Brotatable%3D0%3Bpoints%3D%5B%5B0%2C0.5%5D%2C%5B1%2C0.5%5D%5D%3BportConstraint%3Deastwest%3B%22%20vertex%3D%221%22%20parent%3D%222%22%3E%3CmxGeometry%20y%3D%2226%22%20width%3D%22160%22%20height%3D%2226%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%224%22%20value%3D%22Tweet%22%20style%3D%22swimlane%3BfontStyle%3D0%3Balign%3Dcenter%3BverticalAlign%3Dtop%3BchildLayout%3DstackLayout%3Bhorizontal%3D1%3BstartSize%3D26%3BhorizontalStack%3D0%3BresizeParent%3D1%3BresizeLast%3D0%3Bcollapsible%3D1%3BmarginBottom%3D0%3Brounded%3D0%3Bshadow%3D0%3BstrokeWidth%3D1%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22508%22%20y%3D%22120%22%20width%3D%22160%22%20height%3D%22110%22%20as%3D%22geometry%22%3E%3CmxRectangle%20x%3D%22550%22%20y%3D%22140%22%20width%3D%22160%22%20height%3D%2226%22%20as%3D%22alternateBounds%22%2F%3E%3C%2FmxGeometry%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%225%22%20value%3D%22User_id%22%20style%3D%22text%3Balign%3Dleft%3BverticalAlign%3Dtop%3BspacingLeft%3D4%3BspacingRight%3D4%3Boverflow%3Dhidden%3Brotatable%3D0%3Bpoints%3D%5B%5B0%2C0.5%5D%2C%5B1%2C0.5%5D%5D%3BportConstraint%3Deastwest%3B%22%20vertex%3D%221%22%20parent%3D%224%22%3E%3CmxGeometry%20y%3D%2226%22%20width%3D%22160%22%20height%3D%2226%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%226%22%20value%3D%22title%22%20style%3D%22text%3Balign%3Dleft%3BverticalAlign%3Dtop%3BspacingLeft%3D4%3BspacingRight%3D4%3Boverflow%3Dhidden%3Brotatable%3D0%3Bpoints%3D%5B%5B0%2C0.5%5D%2C%5B1%2C0.5%5D%5D%3BportConstraint%3Deastwest%3Brounded%3D0%3Bshadow%3D0%3Bhtml%3D0%3B%22%20vertex%3D%221%22%20parent%3D%224%22%3E%3CmxGeometry%20y%3D%2252%22%20width%3D%22160%22%20height%3D%2226%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%227%22%20value%3D%22main%22%20style%3D%22text%3Bhtml%3D1%3Balign%3Dleft%3BverticalAlign%3Dmiddle%3Bresizable%3D0%3Bpoints%3D%5B%5D%3Bautosize%3D1%3B%22%20vertex%3D%221%22%20parent%3D%224%22%3E%3CmxGeometry%20y%3D%2278%22%20width%3D%22160%22%20height%3D%2220%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%228%22%20value%3D%22%22%20style%3D%22endArrow%3Dopen%3Bshadow%3D0%3BstrokeWidth%3D1%3BstrokeColor%3D%23000000%3Brounded%3D0%3BendFill%3D1%3BedgeStyle%3DelbowEdgeStyle%3Belbow%3Dvertical%3B%22%20edge%3D%221%22%20source%3D%222%22%20target%3D%224%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%220.5%22%20y%3D%2241%22%20relative%3D%221%22%20as%3D%22geometry%22%3E%3CmxPoint%20x%3D%22380%22%20y%3D%22192%22%20as%3D%22sourcePoint%22%2F%3E%3CmxPoint%20x%3D%22540%22%20y%3D%22192%22%20as%3D%22targetPoint%22%2F%3E%3CmxPoint%20x%3D%22-40%22%20y%3D%2232%22%20as%3D%22offset%22%2F%3E%3C%2FmxGeometry%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%229%22%20value%3D%220..1%22%20style%3D%22resizable%3D0%3Balign%3Dleft%3BverticalAlign%3Dbottom%3BlabelBackgroundColor%3Dnone%3BfontSize%3D12%3B%22%20connectable%3D%220%22%20vertex%3D%221%22%20parent%3D%228%22%3E%3CmxGeometry%20x%3D%22-1%22%20relative%3D%221%22%20as%3D%22geometry%22%3E%3CmxPoint%20y%3D%224%22%20as%3D%22offset%22%2F%3E%3C%2FmxGeometry%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%2210%22%20value%3D%22N%22%20style%3D%22resizable%3D0%3Balign%3Dright%3BverticalAlign%3Dbottom%3BlabelBackgroundColor%3Dnone%3BfontSize%3D12%3B%22%20connectable%3D%220%22%20vertex%3D%221%22%20parent%3D%228%22%3E%3CmxGeometry%20x%3D%221%22%20relative%3D%221%22%20as%3D%22geometry%22%3E%3CmxPoint%20x%3D%22-7%22%20y%3D%224%22%20as%3D%22offset%22%2F%3E%3C%2FmxGeometry%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphModel%3E
Ruby on Rails: 何を作りたいかを考える〜$cdで作業したいフォルダ(作業ディレクトリ)に移動するまで....chapter1-1
①何を作りたいかを考える
とりあえず、タイトルと本文に文書を書いて投稿できるものを作りたい。
そしてユーザーごとにツイートを管理したい。ということにしました。
②$rails new アプリケーション名
アプリケーションを置きたい場所に「$cd ~作りたい場所」で移動し、$rails new アプリケーション名(今回は練習なのでrensyu)→$rails new rensyu1
で、アプリケーションを作るためのフォルダを作りました。
この時、気づいてなかったのですがSequel Proを私の場合入れてあるのでそちらでDBを管理したい場合は、『$rails new rensyu -d mysql』とするといいみたいです。
このコマンドを忘れていて後でSequel Proで作られていないことによって時間を食うことになります。
ちなみに私みたいにやらかしてしまった時の対処法として
1. Gemfileに gem 'mysql2'を追加
2.$bundle install
3.config/database.ymlを開く
4.↓のように変える(rensyuのところは自分の作ってるアプリケーション名に変更)
ちなみにそれぞれの意味は↓らしいです。
adapter: 使用するデータベース種類
encoding: 文字コード
reconnect: 再接続するかどうか
database: データベース名
pool: コネクションプーリングで使用するコネクションの上限
username: ユーザー名
password: パスワード
host: MySQLが動作しているホスト名
5.新規DBを作成
mysql2に変更したから、コマンドで $bundle exec rake db:create をして新たにDBを作成。
6.Sequel Proにアプリケーション名_developmentとアプリケーション名_testがあるか確かめる。
これで行けると思います。
参考ページ: https://qiita.com/reeenapi/items/9fc38c4f2f8186c78288
③$cd rensyuで移動
作りたいアプリを考える〜作業ディレクトリの移動までを書きました。
次回はREADME.mdというファイルでDB設計をしていきます。
chapter1-2に続きます。
以上です。
Ruby on Rails: 挨拶 ...chapter0
プログラミング初心者です。
このブログは学習の記録にしていく予定です。
スクールに通いプログラミングの勉強はしたのですが、全くできない。。。って状態です。
開発のメンバーにはたくさん迷惑かけての卒業。。。
どのくらいできないかというと、、、
↓悲惨な現状
・ビューはなんとなくできるようになった。けど簡単なのしかできない。
作りたいビューは色々なサイトを見て似てるのを見つけたら検証で見てパクってくる。
・昨日まで7つのアクション中5つまでしか分からない。
・アプリケーションの作成でコマンドで $rails new アプリケーション名 これでとりあえずフォルダは作れる。
では、この後どうすればいいの?っていう状態。。。
一体スクールで何を学んできたの?って状態です。
書いていて悲しくなってきました。
頑張りますので温かい目で見守ってください。