rails-model-patterns
RailsのActiveRecordモデルに関する設計パターンや規約を理解し、モデル、関連、バリデーション、スコープ、コールバック、データベーススキーマ設計などの際に、適切な実装方法を提案してくれるSkill。
📜 元の英語説明(参考)
ActiveRecord model patterns and conventions for Rails. Automatically invoked when working with models, associations, validations, scopes, callbacks, or database schema design. Triggers on "model", "ActiveRecord", "association", "has_many", "belongs_to", "validation", "validates", "scope", "callback", "migration", "schema", "index", "foreign key".
🇯🇵 日本人クリエイター向け解説
RailsのActiveRecordモデルに関する設計パターンや規約を理解し、モデル、関連、バリデーション、スコープ、コールバック、データベーススキーマ設計などの際に、適切な実装方法を提案してくれるSkill。
※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o rails-model-patterns.zip https://jpskill.com/download/10048.zip && unzip -o rails-model-patterns.zip && rm rails-model-patterns.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/10048.zip -OutFile "$d\rails-model-patterns.zip"; Expand-Archive "$d\rails-model-patterns.zip" -DestinationPath $d -Force; ri "$d\rails-model-patterns.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
rails-model-patterns.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
rails-model-patternsフォルダができる - 3. そのフォルダを
C:\Users\あなたの名前\.claude\skills\(Win)または~/.claude/skills/(Mac)へ移動 - 4. Claude Code を再起動
⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。
🎯 このSkillでできること
下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。
📦 インストール方法 (3ステップ)
- 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
- 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
- 3. 展開してできたフォルダを、ホームフォルダの
.claude/skills/に置く- · macOS / Linux:
~/.claude/skills/ - · Windows:
%USERPROFILE%\.claude\skills\
- · macOS / Linux:
Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。
詳しい使い方ガイドを見る →- 最終更新
- 2026-05-18
- 取得日時
- 2026-05-18
- 同梱ファイル
- 1
📖 Skill本文(日本語訳)
※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。
Rails Model Patterns
Railsアプリケーションで、構造が整ったActiveRecordモデルを構築するためのパターンです。
このSkillが適用される場面
- 適切な関連付けを持つモデルの設計
- バリデーションとコールバックの実装
- 効率的なスコープとクエリの作成
- 安全なデータベースマイグレーションの記述
- データベースパフォーマンスの最適化
クイックリファレンス
| Pattern | Use When |
|---|---|
belongs_to |
子が親を参照する |
has_many |
親が複数の子を持つ |
has_one |
親が単一の子を持つ |
has_many :through |
中間モデルを介した多対多 |
has_and_belongs_to_many |
単純な多対多(中間モデルの属性なし) |
詳細なドキュメント
- associations.md - 関連付けのパターンとオプション
- validations.md - バリデーションのパターン
- migrations.md - 安全なマイグレーションの実践
モデル構造のベストプラクティス
class User < ApplicationRecord
# 1. Constants
ROLES = %w[admin member guest].freeze
# 2. Associations
belongs_to :organization
has_many :posts, dependent: :destroy
has_many :comments, through: :posts
# 3. Validations
validates :email, presence: true, uniqueness: { case_sensitive: false }
validates :name, presence: true, length: { maximum: 100 }
validates :role, inclusion: { in: ROLES }
# 4. Scopes
scope :active, -> { where(active: true) }
scope :recent, -> { order(created_at: :desc) }
scope :admins, -> { where(role: 'admin') }
# 5. Callbacks (use sparingly)
before_save :normalize_email
# 6. Class methods
def self.find_by_email(email)
find_by(email: email.downcase.strip)
end
# 7. Instance methods
def admin?
role == 'admin'
end
private
def normalize_email
self.email = email.downcase.strip
end
end
主要な原則
Validations
- 可能な限り、組み込みのバリデーターを使用します
- 重要なバリデーションには、データベース制約を追加します
- 複雑なビジネスルールには、カスタムバリデーターを使用します
Associations
- 常に
:dependentオプションを指定します - 双方向の関連付けには
:inverse_ofを使用します - カウントにはカウンターキャッシュを検討します
Scopes
- クエリには、クラスメソッドよりもスコープを優先します
- 複雑なクエリには、スコープをチェーンします
- 異なるモデルからのスコープを組み合わせるには
mergeを使用します
Callbacks
- 控えめに使用し、サービスオブジェクトを優先します
- コールバックは焦点を絞り、シンプルに保ちます
- 外部サービスをトリガーするコールバックは避けます
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開
Rails Model Patterns
Patterns for building well-structured ActiveRecord models in Rails applications.
When This Skill Applies
- Designing models with proper associations
- Implementing validations and callbacks
- Creating efficient scopes and queries
- Writing safe database migrations
- Optimizing database performance
Quick Reference
| Pattern | Use When |
|---|---|
belongs_to |
Child references parent |
has_many |
Parent has multiple children |
has_one |
Parent has single child |
has_many :through |
Many-to-many via join model |
has_and_belongs_to_many |
Simple many-to-many (no join model attributes) |
Detailed Documentation
- associations.md - Association patterns and options
- validations.md - Validation patterns
- migrations.md - Safe migration practices
Model Structure Best Practice
class User < ApplicationRecord
# 1. Constants
ROLES = %w[admin member guest].freeze
# 2. Associations
belongs_to :organization
has_many :posts, dependent: :destroy
has_many :comments, through: :posts
# 3. Validations
validates :email, presence: true, uniqueness: { case_sensitive: false }
validates :name, presence: true, length: { maximum: 100 }
validates :role, inclusion: { in: ROLES }
# 4. Scopes
scope :active, -> { where(active: true) }
scope :recent, -> { order(created_at: :desc) }
scope :admins, -> { where(role: 'admin') }
# 5. Callbacks (use sparingly)
before_save :normalize_email
# 6. Class methods
def self.find_by_email(email)
find_by(email: email.downcase.strip)
end
# 7. Instance methods
def admin?
role == 'admin'
end
private
def normalize_email
self.email = email.downcase.strip
end
end
Key Principles
Validations
- Use built-in validators when possible
- Add database constraints for critical validations
- Custom validators for complex business rules
Associations
- Always specify
:dependentoption - Use
:inverse_offor bidirectional associations - Consider counter caches for counts
Scopes
- Prefer scopes over class methods for queries
- Chain scopes for complex queries
- Use
mergeto combine scopes from different models
Callbacks
- Use sparingly - prefer service objects
- Keep callbacks focused and simple
- Avoid callbacks that trigger external services