0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

🚀 Rails開発効率を爆上げする!知って得するショートカット&生成コマンド完全ガイド

Posted at

Rails productivity


📘 はじめに

「Railsは開発者体験が良い」と言われる理由の一つが、豊富なショートカットコマンドと生成機能です。

しかし、その多くは意外と知られていません。

本記事では、Rails開発の生産性を劇的に向上させる隠れた便利コマンド集を完全ガイドとしてお届けします!


🧱 基本の生成コマンドを再確認

✅ コントローラ生成の裏技

rails g controller Admin::Users index show

⏬ これで以下のファイルが自動生成されます:

app/controllers/admin/users_controller.rb
app/views/admin/users/index.html.erb
app/views/admin/users/show.html.erb
config/routes.rb にルートも追加

✅ モデル生成時のオプション活用

rails g model User name:string:index email:string:uniq birthdate:date admin:boolean:default{false}
  • :index → インデックス付き
  • :uniq → ユニーク制約
  • default{false} → デフォルト値設定

⚡ 知る人ぞ知るショートカット集

💻 コンソール操作を10倍速くする技

user = _                       # 最後の結果を再利用
User.method(:find).source      # メソッドのソース表示
User.column_names              # テーブル構造確認
User.where(active: true).to_sql # SQL表示

🌐 ルーティング確認の達人技

rails routes -g post     # 'post' を含むルートだけ表示
rails routes -c users    # UsersController だけ表示
rails routes -e html     # HTMLフォーマットのみ表示

🧰 データベース操作の時短術

🏗 マイグレーション生成の裏技

rails g migration AddRememberDigestToUsers remember_digest:string

生成される内容:

class AddRememberDigestToUsers < ActiveRecord::Migration[7.0]
  def change
    add_column :users, :remember_digest, :string
  end
end

⏪ スマートなロールバック

rails db:rollback STEP=3
rails db:migrate VERSION=20230801000000

🧪 テストを高速化する魔法のコマンド

🔍 特定のテストだけ実行

rails test test/models/user_test.rb:25

🧱 システムテストのヘッドレス実行

# test/application_system_test_case.rb
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium, using: :headless_chrome
end

🌐 開発サーバー操作の便利技

🔌 ポート&バインディング変更

rails s -p 4000 -b 0.0.0.0

📑 リクエストログを絞り込む

tail -f log/development.log | grep "method=POST"

🧬 生成コマンドのカスタマイズ術

🛠 デフォルトテンプレートを変更

# config/application.rb
config.generators do |g|
  g.test_framework :rspec
  g.assets false
  g.helper false
end

✨ カスタムジェネレータ作成例

# lib/generators/custom_model_generator.rb
class CustomModelGenerator < Rails::Generators::NamedBase
  source_root File.expand_path('templates', __dir__)

  def create_custom_model
    template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
  end
end

🐞 デバッグを加速するプロ技

📍 高度な byebug テクニック

break app/models/user.rb:20
break app/controllers/users_controller.rb:15 if params[:admin]

🧠 メモリ使用量を計測

memory_before = `ps -o rss= -p #{Process.pid}`.to_i
User.all.load
memory_after = `ps -o rss= -p #{Process.pid}`.to_i
puts "Memory used: #{memory_after - memory_before} KB"

🔍 Railsコマンドの知られざる機能

❗ 未使用テーブルを検出

rails db:find_orphans
# ※ gem 'database_consistency' が必要

🧭 ルートの最適化チェック

rails dev:routes

🔧 コマンドラインから直接実行

rails runner 'User.inactive.delete_all'
rails runner -e production 'ReportGenerator.new.run'

📂 生成ファイルをプレビュー

rails g scaffold Post title:string body:text --pretend

✅ まとめ:生産性を上げるためのベストプラクティス

  • カスタマイズ:不要ファイルを生成しない設定を活用
  • 👨‍💻 コンソールマスター_to_sqlで効率的デバッグ
  • 🧪 ターゲット実行:テストやマイグレーションを絞って実行
  • 🌐 ルート最適化rails dev:routesで整理
  • 🧪 ヘッドレステスト:システムテストを高速化

RailsのCLIツールを使いこなせば、1日の開発時間を数時間節約できます。
この記事のテクニックを日々の開発に取り入れ、スピードと品質を両立させましょう!


📚 参考リンク


🏷 推奨タグ(Qiita向け)

Rails, コマンドライン, 開発効率化, ショートカット, テスト

0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?