LoginSignup
5
4

More than 5 years have passed since last update.

Ruby on Railsの最初

Last updated at Posted at 2015-05-24

プロジェクトを作る

data$ rails new new_project

-> いろいろできる。

プロジェクトファイルに移動して、scaffoldでモデル生成

data$ cd new_project
new_project$ rails generate scaffold hoge id:integer name:string memo:text day:date

-> hogeモデルができて、dbに(id,name,memo,day)のテーブルを作る準備ができる。

作ったモデルをdb:migrate

new_project$ rake db:migrate

-> 作ったモデルに合わせてdbにテーブルができる。

new_project$ rails sever

-> サーバー起動して、localhost:3000/hoge/にアクセスして確認
-> 試しに1つレコードを new hoge から生成してみる

できたテーブルを確認

new_project$ rails dbconsole
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>.tables
hoge              schema_migrations

-> hogeテーブルを確認

sqlite>select * from hoge;
1|123|私|あっちらこっちらどっちら|2015-05-24|2015-05-24 04:03:24.555191|2015-05-24 04:03:24.555191

-> sql文でテーブルの中のレコードを確認

sqlite>.quit

-> dbconsole終了

作るのやめたのでモデルを削除

new_project$ rails destroy model hoge

-> hogeモデルが消える

モデルはなくなったけど、dbは?

new_project$ rails dbconsole
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>.tables
hoge              schema_migrations

-> まだhogeテーブルがある。

sqlite>select * from hoge;
1|123|私|あっちらこっちらどっちら|2015-05-24|2015-05-24 04:03:24.555191|2015-05-24 04:03:24.555191

-> 中身もある。

dbに入れたhogeテーブルも削除

new_project$ rake db:drop

-> テーブルを削除。

new_project$ rails dbconsole
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>.tables
sqlite>

-> テーブルを確認したら消えていた。

おわり

とりあえず作って消しては終わり

5
4
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
5
4