rails-debugger
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRails Debugger
Rails 调试工具
Debugging Process
调试流程
1. Gather Information
1. 收集信息
bash
tail -100 log/development.log
bundle exec rspec --format documentation
bin/rails db:migrate:statusbash
tail -100 log/development.log
bundle exec rspec --format documentation
bin/rails db:migrate:status2. Analyze Stack Traces
2. 分析堆栈跟踪
Identify the origin:
- Find the first line in directory
app/ - Note the file, line number, and method
Common patterns:
| Error | Likely Cause |
|---|---|
| Missing association, nil return |
| ID doesn't exist, scoping issue |
| Validation failed |
| Required param not sent |
| Missing require, typo |
| File not found, autoload path issue |
定位问题来源:
- 找到目录中的第一行报错信息
app/ - 记录文件、行号和方法
常见错误模式:
| 错误 | 可能原因 |
|---|---|
| 关联缺失,返回nil |
| ID不存在,作用域问题 |
| 验证失败 |
| 缺少必填参数 |
| 缺少引用,拼写错误 |
| 文件未找到,自动加载路径问题 |
3. Check Common Issues
3. 检查常见问题
Database:
bash
bin/rails db:migrate:status
bin/rails db:schema:dumpDependencies:
bash
bundle check
bundle install数据库相关:
bash
bin/rails db:migrate:status
bin/rails db:schema:dump依赖相关:
bash
bundle check
bundle install4. Isolate the Problem
4. 隔离问题
Reproduce in console:
ruby
user = User.find(123)
user.some_method # Does it fail here?Binary search:
- Comment out half the code
- Does error persist?
- Narrow down
在控制台中复现:
ruby
user = User.find(123)
user.some_method # 此处是否会报错?二分排查法:
- 注释掉一半代码
- 错误是否仍然存在?
- 逐步缩小范围
5. Check Recent Changes
5. 检查近期变更
bash
git log --oneline -20
git log -p --follow app/models/user.rb
git diff HEAD~5 app/models/user.rbbash
git log --oneline -20
git log -p --follow app/models/user.rb
git diff HEAD~5 app/models/user.rbDebugging Techniques
调试技巧
Hypothesis Testing
假设验证法
- Form specific, testable theories
- Design minimal tests to prove/disprove
- Document what you've ruled out
- 提出具体、可验证的假设
- 设计最小化测试来证明/推翻假设
- 记录已排除的可能性
State Inspection
状态检查
ruby
Rails.logger.debug { "DEBUG: user=#{user.inspect}" }
binding.irb # Pause here (Rails 7+)ruby
Rails.logger.debug { "DEBUG: user=#{user.inspect}" }
binding.irb # 在此处暂停(Rails 7+)Common Rails Issues
常见Rails问题
N+1 Queries
N+1 查询问题
bash
grep "SELECT" log/development.log | sort | uniq -c | sort -rnFix:
User.includes(:posts)bash
grep "SELECT" log/development.log | sort | uniq -c | sort -rn修复方案:
User.includes(:posts)Routing Issues
路由问题
bash
bin/rails routes | grep users
bin/rails routes -c usersbash
bin/rails routes | grep users
bin/rails routes -c usersCallback Issues
回调问题
ruby
User._create_callbacks.map(&:filter)
User._save_callbacks.map(&:filter)ruby
User._create_callbacks.map(&:filter)
User._save_callbacks.map(&:filter)Bug Report Validation
Bug报告验证
Classification
分类
| Status | Meaning |
|---|---|
| Confirmed Bug | Reproduced with clear deviation |
| Cannot Reproduce | Unable to reproduce |
| Not a Bug | Behavior is correct per spec |
| Data Issue | Problem with specific data |
| 状态 | 含义 |
|---|---|
| Confirmed Bug | 已复现,存在明显偏差 |
| Cannot Reproduce | 无法复现 |
| Not a Bug | 行为符合规范 |
| Data Issue | 特定数据存在问题 |
Output Format
输出格式
- Reproduction Status - Confirmed / Cannot Reproduce / Not a Bug
- Root Cause - What's actually wrong (explain why)
- Evidence - Specific logs, traces, or code
- Fix - Minimal code changes
- Prevention - How to avoid similar issues
- Verification - Commands/tests to confirm fix
- 复现状态 - 已确认/无法复现/不是Bug
- 根本原因 - 实际问题所在(解释为什么会发生)
- 证据 - 具体日志、堆栈跟踪或代码
- 修复方案 - 最小化代码变更
- 预防措施 - 如何避免类似问题
- 验证方式 - 确认修复的命令/测试