Creates Rails models using TDD approach - test first, then migration, then model. Use when creating new models, adding model validations, defining associations, or setting up database tables.
This skill creates models the TDD way:
Model Creation Progress:
- [ ] Step 1: Define requirements (attributes, validations, associations)
- [ ] Step 2: Create model test (RED)
- [ ] Step 3: Create fixtures
- [ ] Step 4: Run test (should fail - no model/table)
- [ ] Step 5: Generate migration
- [ ] Step 6: Run migration
- [ ] Step 7: Create model file (empty)
- [ ] Step 8: Run test (should fail - no validations)
- [ ] Step 9: Add validations and associations
- [ ] Step 10: Run test (GREEN)
Before writing code, define the model:
## Model: [ModelName]
### Table: [table_name]
### Attributes
| Name | Type | Constraints | Default |
|------|------|-------------|---------|
| name | string | required, unique | - |
| email | string | required, unique, email format | - |
| status | integer | enum | 0 (pending) |
| organization_id | bigint | foreign key | - |
### Associations
- belongs_to :organization
- has_many :posts, dependent: :destroy
- has_one :profile, dependent: :destroy
### Validations
- name: presence, uniqueness, length(max: 100)
- email: presence, uniqueness, format(email)
- status: inclusion in enum values
### Scopes
- active: status = active
- recent: ordered by created_at desc
- by_organization(org): where organization_id = org.id
### Instance Methods
- full_name: combines first_name and last_name
- active?: checks if status is active
### Callbacks
- before_save :normalize_email
- after_create :send_welcome_email
Location: test/models/[model_name]_test.rb
# frozen_string_literal: true
require "test_helper"
class ModelNameTest < ActiveSupport::TestCase
# === Associations ===
test "belongs to organization" do
model = model_names(:one)
assert_respond_to model, :organization
assert_instance_of Organization, model.organization
end
test "has many posts" do
model = model_names(:one)
assert_respond_to model, :posts
end
# === Validations ===
test "requires name" do
model = ModelName.new(name: nil)
assert_not model.valid?
assert_includes model.errors[:name], "can't be blank"
end
test "requires unique email (case insensitive)" do
existing = model_names(:one)
model = ModelName.new(email: existing.email.upcase)
assert_not model.valid?
assert_includes model.errors[:email], "has already been taken"
end
test "validates name length max 100" do
model = ModelName.new(name: "a" * 101)
assert_not model.valid?
assert model.errors[:name].any? { |e| e.include?("too long") }
end
# === Scopes ===
test ".active returns only active records" do
active_record = model_names(:active_one)
inactive_record = model_names(:inactive_one)
results = ModelName.active
assert_includes results, active_record
assert_not_includes results, inactive_record
end
# === Instance Methods ===
test "#full_name returns combined name" do
model = ModelName.new(first_name: "John", last_name: "Doe")
assert_equal "John Doe", model.full_name
end
end
Location: test/fixtures/[model_name_plural].yml
# test/fixtures/model_names.yml