Ruby on Rails Case Studies & Practical Application 5 — Questions and Answers
Question 1: A Rails app's Lighthouse score shows a 4-second LCP. The hero image is a 3MB PNG. Best Rails-side fix?
- Enable HTTP/2 on Puma to parallelize asset loading
- Use Active Storage variants to serve resized, compressed WebP images (Correct answer)
- Preload the image in the <head> with a link tag
- Store the image in Redis to reduce disk I/O
Correct answer: Use Active Storage variants to serve resized, compressed WebP images
Active Storage variants transform images on demand to appropriate sizes and modern formats like WebP, drastically reducing payload.
Question 2: A Rails app's logs show 'connection pool timeout' errors under load. What does this indicate and how do you fix it?
- The database server is out of disk space; add more storage
- More threads are requesting DB connections than the pool size allows; align pool size with thread count (Correct answer)
- ActiveRecord is not closing connections after requests; restart the server
- PostgreSQL's max_connections is set too high; lower it
Correct answer: More threads are requesting DB connections than the pool size allows; align pool size with thread count
Pool timeout means all pool connections are in use; align database.yml pool size with Puma thread count.
Question 3: A public-facing Rails API returns 500 errors when clients send a string where an integer is expected. Best fix?
- Rescue TypeError globally and return a 400 response
- Validate and coerce params using a schema object before passing to the model (Correct answer)
- Cast params with .to_i silently throughout controllers
- Add integration tests for invalid param types and deploy a hotfix
Correct answer: Validate and coerce params using a schema object before passing to the model
Explicit schema validation at the controller boundary rejects malformed input early with a clean 422 response.
Question 4: A Rails app must generate PDF invoices for thousands of orders. Running wkhtmltopdf synchronously times out. Correct approach?
- Increase Rails request timeout to 5 minutes
- Generate PDFs in a background job and store via Active Storage; serve a download link when ready (Correct answer)
- Use a lighter PDF gem like Prawn instead
- Pre-generate all PDFs nightly via a cron task
Correct answer: Generate PDFs in a background job and store via Active Storage; serve a download link when ready
Async generation with Active Storage decouples PDF creation from the HTTP request, providing a scalable, non-blocking user experience.
Question 5: Your Rails app has a complex discount calculation used in both the order total and the cart summary. Where should this logic live?
- Inline in both the OrdersController and the CartsController
- In a view helper so it can be reused across templates
- In a service object or model method called from both controllers (Correct answer)
- In a database stored procedure for performance
Correct answer: In a service object or model method called from both controllers
A service object or model method centralizes the logic, making it testable and preventing duplication across controllers.
Question 6: A Rails app renders user comments with raw(comment.body). Users report a stored XSS attack. Fix?
- Escape HTML on input using strip_tags before saving
- Use ERB's default auto-escaping by rendering with <%= comment.body %> instead of raw() (Correct answer)
- Add a Content Security Policy header to block inline scripts
- Sanitize output with a regex that strips <script> tags
Correct answer: Use ERB's default auto-escaping by rendering with <%= comment.body %> instead of raw()
Rails ERB auto-escapes strings with <%= %>; using raw() bypasses this protection and must be avoided with user-supplied content.
Question 7: A Rails app must schedule recurring billing on the 1st of every month. Which approach is most appropriate?
- Use the whenever gem to trigger a Rake task via system cron
- Use Sidekiq-Cron or a similar scheduler that enqueues billing jobs on schedule (Correct answer)
- Build a custom loop with sleep in a dedicated thread inside Puma
- Schedule billing via the database using pg_cron
Correct answer: Use Sidekiq-Cron or a similar scheduler that enqueues billing jobs on schedule
Sidekiq-Cron integrates scheduling with Sidekiq's reliable job processing, providing retries and visibility that plain cron lacks.
A Rails app's Lighthouse score shows a 4-second LCP.
The hero image is a 3MB PNG.
Best Rails-side fix?