Ruby on Rails Case Studies & Practical Application 4 — Questions and Answers
Question 1: A Rails app's test suite takes 45 minutes to run. The biggest cost is database setup per test. Which strategy best reduces this?
- Switch from RSpec to Minitest for faster execution
- Use database_cleaner with transaction strategy instead of truncation (Correct answer)
- Mock all ActiveRecord models to avoid hitting the database
- Shard tests across multiple CI workers in parallel
Correct answer: Use database_cleaner with transaction strategy instead of truncation
The transaction strategy wraps each test in a rolled-back transaction, avoiding expensive truncation and dramatically cutting test time.
Question 2: You need to add full-text search to a Rails blog with PostgreSQL. What is the most pragmatic first choice?
- Integrate Elasticsearch via the elasticsearch-rails gem
- Use pg_search gem leveraging PostgreSQL's built-in full-text search (Correct answer)
- Build a custom search with LIKE queries and wildcards
- Add Solr as an external search service
Correct answer: Use pg_search gem leveraging PostgreSQL's built-in full-text search
pg_search uses PostgreSQL's native tsvector/tsquery, giving solid full-text search with no additional infrastructure.
Question 3: A Rails app must enforce that only admins can access the /admin namespace. Where is this best implemented?
- Check current_user.admin? in every admin controller action
- Add a before_action in an AdminController base class that all admin controllers inherit from (Correct answer)
- Use Rack middleware to block /admin routes for non-admins
- Add an authentication check directly in config/routes.rb
Correct answer: Add a before_action in an AdminController base class that all admin controllers inherit from
A shared before_action in a base AdminController ensures the check runs for every admin action without duplicating code.
Question 4: A Rails app uses sessions to store shopping carts, but carts are lost when the session cookie expires. What is a better approach?
- Increase the session expiry to one year via config.session_store
- Store the cart in the database associated with the user or a guest token (Correct answer)
- Serialize the cart to localStorage and sync on page load
- Use ActionCable to sync cart state across requests
Correct answer: Store the cart in the database associated with the user or a guest token
Database-backed carts persist across sessions, devices, and expiry, and can be recovered after login.
Question 5: Your Rails app sends welcome emails after user registration. During a spike, the email queue backs up for hours. What is the correct scaling fix?
- Increase Action Mailer delivery timeout settings
- Add more Sidekiq workers or increase concurrency to clear the queue faster (Correct answer)
- Switch to synchronous email delivery to avoid queue buildup
- Batch welcome emails and send them once per hour
Correct answer: Add more Sidekiq workers or increase concurrency to clear the queue faster
Adding Sidekiq workers or increasing concurrency scales throughput to match the queue depth during traffic spikes.
Question 6: A Rails app allows users to search other users by name. A LIKE '%name%' query is slow on 2 million rows. Best fix?
- Add a B-tree index on users.name
- Use trigram indexes (pg_trgm) with GIN or GiST to support LIKE queries efficiently (Correct answer)
- Cache the entire users table in Redis
- Limit search results to 10 and paginate aggressively
Correct answer: Use trigram indexes (pg_trgm) with GIN or GiST to support LIKE queries efficiently
pg_trgm trigram indexes support leading-wildcard LIKE and ILIKE queries efficiently, which B-tree indexes cannot.
Question 7: A SaaS app must accept webhook events from Stripe. What must be validated on every incoming webhook request?
- That the request IP is in Stripe's IP allowlist
- The Stripe-Signature header using the webhook endpoint's signing secret (Correct answer)
- That the webhook payload is under 1MB in size
- That the event ID has not been seen in the last 24 hours only
Correct answer: The Stripe-Signature header using the webhook endpoint's signing secret
Verifying the HMAC signature in Stripe-Signature using your signing secret ensures the request genuinely came from Stripe.
A Rails app's test suite takes 45 minutes to run.
The biggest cost is database setup per test.
Which strategy best reduces this?