Ruby on Rails Research & Evidence-Based Practice 2 — Questions and Answers
Question 1: Which Rails tool is best suited for benchmarking controller action performance to make evidence-based optimization decisions?
- rails benchmark
- Rack::MiniProfiler
- ActiveSupport::Benchmarkable (Correct answer)
- Bullet gem
Correct answer: ActiveSupport::Benchmarkable
ActiveSupport::Benchmarkable provides the `benchmark` method to measure and log execution time of code blocks directly in Rails.
Question 2: When evaluating database query performance in a Rails app, which log metric is the primary evidence of N+1 query problems?
- Response time headers
- Repeated identical SQL SELECT statements in logs (Correct answer)
- Memory usage spikes
- CPU utilization in Prometheus
Correct answer: Repeated identical SQL SELECT statements in logs
N+1 problems appear as the same SQL SELECT pattern repeated many times in Rails logs, once per associated record.
Question 3: A Rails team wants to validate whether eager loading reduces page load time. Which gem provides automated N+1 detection in test suites?
- FactoryBot
- Bullet (Correct answer)
- DatabaseCleaner
- Capybara
Correct answer: Bullet
The Bullet gem detects N+1 queries and unused eager loading during test runs, alerting developers to performance issues.
Question 4: Which Rails environment is most appropriate for running reproducible performance experiments with production-like data?
- development
- test
- staging (Correct answer)
- sandbox
Correct answer: staging
Staging mirrors production configuration, making it the most reliable environment for evidence-based performance experiments.
Question 5: What does the `explain` method on an ActiveRecord relation return, and why is it used in research-driven optimization?
- A human-readable SQL summary string
- The database query execution plan (Correct answer)
- A count of affected rows
- The SQL string without executing it
Correct answer: The database query execution plan
`explain` returns the database's execution plan (e.g., EXPLAIN output), showing index usage and scan types for optimization research.
Question 6: When A/B testing a Rails feature, which approach best isolates the variable being measured?
- Deploy both versions simultaneously and compare logs
- Change multiple features at once for faster learning
- Hold all other variables constant and change only the target feature (Correct answer)
- Use different servers for each variant
Correct answer: Hold all other variables constant and change only the target feature
Controlling all variables except the one being tested is the foundation of valid A/B experimentation in any context.
Question 7: Which ActiveRecord method allows developers to profile the number of SQL queries executed during a test to establish baseline evidence?
- ActiveRecord::QueryCounter
- assert_queries (Correct answer)
- ActiveRecord::Base.count_queries
- QueryAnalyzer.run
Correct answer: assert_queries
`assert_queries(n)` in Rails tests verifies that exactly n SQL queries were executed, serving as a performance regression baseline.
Which Rails tool is best suited for benchmarking controller action performance to make evidence-based optimization decisions?