-
Notifications
You must be signed in to change notification settings - Fork 0
Rails Console
Railsアプリケーションとコマンドラインでやりとりするためにrails consoleコマンドを用います。DBベース内に新しくデータと作成したり、削除したり、一般的な操作が可能です。また、自分が思いついたメソッドが正しく動くのかを確認するためにも役に立ちます。
例えばNoteコントローラのindexアクションに書くメソッドNote.all
には何のデータが入っているのかを知ることができ、Railsアプリの開発においてはとても便利で必要不可欠なコマンドです。
###データを作成・削除してみよう!
Rails Consoleコマンドでコマンドラインを起動し、Noteテーブルにデータの作成とデータの削除を行ってみましょう!あらかじめ、localhost:3000/notes/new
にて幾つかのデータを作成しておいてください。
それではコマンドラインで起動してNoteデーブルにどのようなデータが入っているのかを確認しましょう!
rails console
irb(main):001:0> Note.all
#実行結果
=> #<ActiveRecord::Relation [
#<Note id: 1, title: "Takeshi's Plan", body: "Lunch with Uehara", date: "2014-11-20 10:20:00", created_at: "2014-11-20 05:19:26", updated_at: "2014-11-20 05:20:57">,
#<Note id: 2, title: "hello", body: "hello2", date: "2014-11-23 13:34:00", created_at: "2014-11-23 13:34:07", updated_at: "2014-11-23 13:34:07">,
#<Note id: 3, title: "testing", body: "testing-body", date: "2014-10-23 13:34:00", created_at: "2014-11-23 13:34:26", updated_at: "2014-11-23 13:34:26">
]>
今現在のNoteテーブルには3つのエントリーが登録されていることが確認できます。見にくいのですが、id
,title
,body
,date
,created_at
,updated_at
のカラムのデータが入っていますね。以下の画面と同じ内容ですね!
このようにGUIとCUIの違いとも言えるでしょう。
それではNoteデーブルにデータを作成していきましょう。今回には以下のようなデータを追加します。
カラム名 | 内容 |
---|---|
title | takeshi's life |
body | Greatest Life Ever!!!! |
date | 2014-11-13 |
コマンドライン上で以下を入力してください。*rails console
を起動している状態
#新しいデータを作成することを宣言し、noteオブジェクトに格納
note = Note.new
#それぞれのカラムにデータを追加
note.title = "takeshi's life"
note.body = "Greatest Life Ever!!!!"
note.date = "2014-11-13"
#保存!
note.save
#実行結果
irb(main):001:0> note = Note.new
=> #<Note id: nil, title: nil, body: nil, date: nil, created_at: nil, updated_at: nil>
irb(main):002:0> note.title = "takeshi's life"
=> "takeshi's life"
irb(main):003:0> note.body = "Greatest Life Ever!!!!"
=> "Greatest Life Ever!!!!"
irb(main):004:0> note.date = "2014-11-13"
=> "2014-11-13"
irb(main):005:0> note.save
(0.4ms) begin transaction
SQL (1.9ms) INSERT INTO "notes" ("body", "created_at", "date", "title", "updated_at") VALUES (?, ?, ?, ?, ?) [["body", "Greatest Life Ever!!!!"], ["created_at", "2014-12-13 15:58:42.021311"], ["date", "2014-11-13 00:00:00.000000"], ["title", "takeshi's life"], ["updated_at", "2014-12-13 15:58:42.021311"]]
(1.1ms) commit transaction
=> true
ここでNote.all
メソッドを用いてデータが本当に作成できたのか確認してみましょう!
irb(main):006:0> Note.all
Note Load (8.5ms) SELECT "notes".* FROM "notes"
=> #<ActiveRecord::Relation [
#<Note id: 1, title: "Takeshi's Plan", body: "Lunch with Uehara", date: "2014-11-20 10:20:00", created_at: "2014-11-20 05:19:26", updated_at: "2014-11-20 05:20:57">,
#<Note id: 2, title: "hello", body: "hello2", date: "2014-11-23 13:34:00", created_at: "2014-11-23 13:34:07", updated_at: "2014-11-23 13:34:07">,
#<Note id: 3, title: "testing", body: "testing-body", date: "2014-10-23 13:34:00", created_at: "2014-11-23 13:34:26", updated_at: "2014-11-23 13:34:26">,
#<Note id: 4, title: "takeshi's life", body: "Greatest Life Ever!!!!", date: "2014-11-13 00:00:00", created_at: "2014-12-13 15:58:42", updated_at: "2014-12-13 15:58:42">]>**
新しくidが4としてデータベースに入っていますね!
それではこのidが4の新しく作成したデータを削除してみましょう!
コマンドライン上で以下を入力してください。*rails console
を起動している状態
#idが4のデータをnoteオブジェクトに格納
note = Note.find(4)
#削除!
note.destroy
#実行結果
irb(main):001:0> note = Note.find(4)
Note Load (0.3ms) SELECT "notes".* FROM "notes" WHERE "notes"."id" = ? LIMIT 1 [["id", 4]]
=> #<Note id: 4, title: "takeshi's life", body: "Greatest Life Ever!!!!", date: "2014-11-13 00:00:00", created_at: "2014-12-13 15:58:42", updated_at: "2014-12-13 15:58:42">
irb(main):002:0> note.destroy
(0.2ms) begin transaction
SQL (0.5ms) DELETE FROM "notes" WHERE "notes"."id" = ? [["id", 4]]
(9.3ms) commit transaction
=> #<Note id: 4, title: "takeshi's life", body: "Greatest Life Ever!!!!", date: "2014-11-13 00:00:00", created_at: "2014-12-13 15:58:42", updated_at: "2014-12-13 15:58:42">
ここでNote.all
メソッドを用いてデータが削除されたのかを確認しましょう。
irb(main):003:0> Note.all
Note Load (8.4ms) SELECT "notes".* FROM "notes"
=> #<ActiveRecord::Relation [
#<Note id: 1, title: "Takeshi's Plan", body: "Lunch with Uehara", date: "2014-11-20 10:20:00", created_at: "2014-11-20 05:19:26", updated_at: "2014-11-20 05:20:57">,
#<Note id: 2, title: "hello", body: "hello2", date: "2014-11-23 13:34:00", created_at: "2014-11-23 13:34:07", updated_at: "2014-11-23 13:34:07">,
#<Note id: 3, title: "testing", body: "testing-body", date: "2014-10-23 13:34:00", created_at: "2014-11-23 13:34:26", updated_at: "2014-11-23 13:34:26">]>
しっかりとidが4のエントリーが削除されていることがわかります!
###データを条件検索しよう!
以前も行いましたが、条件検索を行うときにはwhereメソッドを用います。他にもorder, limit, takeなど様々なメソッドが存在します(詳しくはActive Record Query Interface)。実は第6回目の講義の講座課題でやったデータの条件検索もrails consoleコマンドで起動したコマンドラインでも確認することができるのです。
幾つか簡単な例を見ていきましょう。
- idが1のエントリーを取得
Note.where(:id => 1)
#実行結果
irb(main):004:0> Note.where(:id => 1)
Note Load (2.3ms) SELECT "notes".* FROM "notes" WHERE "notes"."id" = 1
=> #<ActiveRecord::Relation [
#<Note id: 1, title: "Takeshi's Plan", body: "Lunch with Uehara", date: "2014-11-20 10:20:00", created_at: "2014-11-20 05:19:26", updated_at: "2014-11-20 05:20:57">
]>
- エントリーをdateを基準に並び替えをして、一番最近入力したものを一番上から表示
Note.all.order('date DESC')
#実行結果
irb(main):005:0> Note.all.order('date DESC')
Note Load (8.4ms) SELECT "notes".* FROM "notes" ORDER BY date DESC
=> #<ActiveRecord::Relation [
#<Note id: 2, title: "hello", body: "hello2", date: "2014-11-23 13:34:00", created_at: "2014-11-23 13:34:07", updated_at: "2014-11-23 13:34:07">,
#<Note id: 1, title: "Takeshi's Plan", body: "Lunch with Uehara", date: "2014-11-20 10:20:00", created_at: "2014-11-20 05:19:26", updated_at: "2014-11-20 05:20:57">,
#<Note id: 3, title: "testing", body: "testing-body", date: "2014-10-23 13:34:00", created_at: "2014-11-23 13:34:26", updated_at: "2014-11-23 13:34:26">]>
- id順で上2つのエントリーを取得
Note.take(2)
irb(main):006:0> Note.take(2)
Note Load (8.3ms) SELECT "notes".* FROM "notes" LIMIT 2
=> [
#<Note id: 1, title: "Takeshi's Plan", body: "Lunch with Uehara", date: "2014-11-20 10:20:00", created_at: "2014-11-20 05:19:26", updated_at: "2014-11-20 05:20:57">,
#<Note id: 2, title: "hello", body: "hello2", date: "2014-11-23 13:34:00", created_at: "2014-11-23 13:34:07", updated_at: "2014-11-23 13:34:07">
]
- [超複雑] エントリーをdateを基準に並び替えをして、一番最近入力したものを一番上から表示し、その中での11/10以降のエントリーを検索し、その中の一番最近のエントリーのタイトルを表示
Note.all.order('date DESC').where('created_at > 2014-11-10').first.title
#実行結果
irb(main):015:0> Note.all.order('date DESC').where('created_at > 2014-11-10').first.title
Note Load (0.3ms) SELECT "notes".* FROM "notes" WHERE (created_at > 2014-11-10) ORDER BY date DESC LIMIT 1
=> "hello"
このようにRails Consoleではデータを作成、削除をすることができ、かつ条件付きでのエントリーの検索をすることができます。
いや〜便利ですな!
ちなみにrails consoleの起動を終了させるためにはexit
と入力をしてください。
irb(main):001:0> exit
Copyright (C) Catal,Inc. All Rights Reserved.