Ruby on Rails Case Studies & Practical Application 2 — Questions and Answers
Question 1: A Rails e-commerce app experiences N+1 queries when displaying an order list with customer names. Which fix is most appropriate?
- Add an index on orders.customer_id
- Use Order.includes(:customer) in the controller (Correct answer)
- Cache the customer names in a Redis store
- Denormalize customer_name into the orders table
Correct answer: Use Order.includes(:customer) in the controller
includes(:customer) eager-loads the association, eliminating the N+1 by fetching all customers in one query.
Question 2: You need to allow users to upload profile photos in a Rails app. Which Rails built-in handles file attachment handling?
- Paperclip
- CarrierWave
- Active Storage (Correct answer)
- shrine
Correct answer: Active Storage
Active Storage ships with Rails 5.2+ and is the officially recommended solution for file attachments.
Question 3: A multi-tenant SaaS app must scope all database queries to the current tenant to prevent data leaks. What is the safest Rails pattern?
- Set a global variable with the tenant ID and filter manually
- Use default_scope with a lambda that reads current_tenant
- Use a gem like acts_as_tenant that applies scoping at the model level (Correct answer)
- Separate databases per tenant and switch connections per request
Correct answer: Use a gem like acts_as_tenant that applies scoping at the model level
acts_as_tenant automatically applies tenant scopes to all queries, reducing the risk of missed manual filters.
Question 4: Your Rails API is slow because email sending is blocking web requests. What is the correct architectural fix?
- Increase the number of Puma threads
- Move email sending to an async job queue via Sidekiq or Active Job (Correct answer)
- Render the email in a partial to reduce view time
- Cache email content with Rails.cache
Correct answer: Move email sending to an async job queue via Sidekiq or Active Job
Offloading email delivery to a background job keeps web requests fast and non-blocking.
Question 5: A Rails app must support both JSON API and HTML responses from the same controller action. Which Rails feature handles this cleanly?
- before_action with format checks
- respond_to blocks with format.html and format.json (Correct answer)
- Separate controllers for each format
- Middleware that inspects Accept headers and routes to different actions
Correct answer: respond_to blocks with format.html and format.json
respond_to blocks let a single action render different formats based on the client's Accept header or URL extension.
Question 6: A startup's Rails app stores user passwords in plain text. What is the correct remediation using Rails conventions?
- Encrypt passwords with AES-256 before storing
- Use has_secure_password with bcrypt and force existing users to reset passwords (Correct answer)
- Add SSL to the database connection
- Store passwords in environment variables instead of the database
Correct answer: Use has_secure_password with bcrypt and force existing users to reset passwords
has_secure_password uses bcrypt hashing; existing plain-text passwords must be expired and users prompted to reset them.
Question 7: An ActiveRecord callback in an after_create hook raises an exception. What happens to the record that was just created?
- The record remains saved; the exception is logged but swallowed
- The transaction rolls back and the record is not persisted (Correct answer)
- The record is saved but marked as invalid
- Rails retries the callback up to three times before raising
Correct answer: The transaction rolls back and the record is not persisted
after_create runs inside the same transaction as the INSERT; an unhandled exception rolls the entire transaction back.
A Rails e-commerce app experiences N+1 queries when displaying an order list with customer names.
Which fix is most appropriate?