LoginSignup
1
0

More than 3 years have passed since last update.

【Rails】SQliteでのDBの内容確認周りのコマンドまとめ(追加していく所存)

Last updated at Posted at 2020-03-29

【Rails】SQliteでのDBの内容確認周りのコマンド

モデルのカラム数の確認

terminal
>rails console

>モデル名.new

↓こんな感じ
ちゃんとカラム作れてるかとかの確認に良きです
スクリーンショット 2020-03-29 14.11.43.png

モデルの内容確認(全ては見れません)

terminal
> rails console

> モデル名.all

↓こんな感じ
スクリーンショット 2020-03-29 14.15.08.png

モデルの特定のidをもつデータのもつ内容の確認

terminal
> rails console

> モデル名.find(特定のid)

↓こんな感じ
スクリーンショット 2020-03-29 16.16.49.png

モデルの特定のカラム内容をもつ行の確認

terminal
> rails console

> モデル名.find_by(カラム名: "文字列や数字")

↓こんな感じ
スクリーンショット 2020-03-29 17.22.40.png

投稿の数やユーザーの数の確認

terminal
> rails console

> モデル名.select("調べたいカラム名").count

例えば、Userモデルに登録されているユーザーの数を知りたいとき

terminal
> rails c

> User.select("id").count

この結果
irb(main):007:0> User.select("id").count
   (1.5ms)  SELECT COUNT("users"."id") FROM "users"
=> 1
↑ユーザーの数が1だとわかる

例えば、Postモデルに登録されている投稿の数を知りたいとき

terminal
> rails c

> Post.select("id").count

この結果
irb(main):007:0> User.select("id").count
   (1.5ms)  SELECT COUNT("posts"."id") FROM "users"
=> 27
↑ユーザーの数が1だとわかる
1
0
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
1
0