LoginSignup
4
1

More than 5 years have passed since last update.

rails / rake / sqlite3コマンド

Last updated at Posted at 2018-11-13

tl;dr

railsの学習中に見かけた各種コマンドをメモしておく。

rails/rakeコマンド

$ rails
The most common rails commands are:
 generate     Generate new code (short-cut alias: "g")
 console      Start the Rails console (short-cut alias: "c")
 server       Start the Rails server (short-cut alias: "s")
 test         Run tests except system tests (short-cut alias: "t")
 test:system  Run system tests
 dbconsole    Start a console for the database specified in config/database.yml
              (short-cut alias: "db")
 new          Create a new Rails application. "rails new my_app" creates a
              new application called MyApp in "./my_app"

All commands can be run with -h (or --help) for more information.

Rails:
  console
  dbconsole
  destroy
  generate
  new
  runner
  secrets:edit
  secrets:setup
  server
  test
  version

Rake:
  about
  app:template
  app:update
  assets:clean[keep]
  assets:clobber
  assets:environment
  assets:precompile
  cache_digests:dependencies
  cache_digests:nested_dependencies
  db:create
  db:drop
  db:environment:set
  db:fixtures:load
  db:migrate
  db:migrate:status
  db:rollback
  db:schema:cache:clear
  db:schema:cache:dump
  db:schema:dump
  db:schema:load
  db:seed
  db:setup
  db:structure:dump
  db:structure:load
  db:version
  dev:cache
  initializers
  log:clear
  middleware
  notes
  notes:custom
  restart
  routes
  secret
  stats
  test
  test:db
  test:system
  time:zones[country_or_offset]
  tmp:clear
  tmp:create
  yarn:install

サーバ起動

Webサーバ(Puma)を起動する。

ターミナル
$ rails s

ルーティング表示

config/routes.rbに定義されているルーティングを表示する。

ターミナル
$ rake routes

データベース作成

config/database.ymlに定義されているデータベースを作成する。

ターミナル
$ rake db:create

マイグレーション実行

db/migrate/***.rbに定義されている内容を実行します。

$ rake db:migrate

コントローラ作成

新規にコントローラを作成する時に関連ファイルもまとめて作成してくれます。

ターミナル
$ rails g controller users#index

モデル作成

新規にモデルを作成する時に関連ファイルもまとめて作成してくれます。

ターミナル
$ rails g model User name:string message:text

Scaffoldingでアプリの雛形作成

ターミナル
$ rails g scaffold diary title:string body:text

DBコンソール起動

データベースの操作を行いたい場合に便利そうです。

$ rails dbconsole
sqlite> select * from users limit 10;
...
sqlite> .exit

テストデータ作成

test/fixtures/***.ymlに定義されている内容でテストデータを作成します。

ターミナル
$ rake db:fixtures:load

Railsコンソール起動

rails自体を実行してみたい場合に便利そうです。

ターミナル
$ rails c

irb(main):001:0> User.all
irb(main):002:0> user = User.find(1)
irb(main):003:0> user.name
irb(main):004:0> user.message
irb(main):005:0> user = User.new
irb(main):006:0> user.id = 3
irb(main):007:0> user.name = 'this is name.'
irb(main):008:0> user.message = 'this is message.'
irb(main):009:0> user.save
irb(main):010:0> user = User.find(3)
irb(main):011:0> user.name = 'it is name.'
irb(main):012:0> user.save
irb(main):013:0> user = User.find(3)
irb(main):014:0> user.destroy
irb(main):015:0> exit

sqlite3

railsのデフォルトDBの操作方法

SQLインタプリタ起動

$ sqlite3 db/hoge.sqlite3
sqlite> ...
sqlite> .exit

ダンプ

$ sqlite3 db/hoge.sqlite3 .dump

テーブル一覧の確認

sqlite> .tables
ar_internal_metadata users schema_migrations   

テーブル定義の確認

sqlite> .schema users
CREATE TABLE IF NOT EXISTS "users" (
  "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
  "name" varchar, 
  "message" text, 
  "created_at" datetime NOT NULL, 
  "updated_at" datetime NOT NULL
);

ダンプ (テーブル指定)

sqlite> .dump users

インタプリタ終了

sqlite> .exit
sqlite> .quite
4
1
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
4
1