Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

Ruby on Rails Module 11

Active Record in depth interview questions

Interview questions on active Record in depth: short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.

4 questions: 1 warm-up and 3 with model answers · Levels: Fresher to staff

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher

    How do you spot an N+1 query while a page is open in development?

    Open the terminal running the server and read the SELECTs the request logged. An N+1 shows up as a cluster of identical statements with different bound values, one per row of the list on the page: SELECT "teachers".* FROM "teachers" WHERE "teachers"."id" = ? LIMIT ? nine times in a row, each with a different ?, right after the one SELECT that read the batches. With config.active_record.query_log_tags_enabled on (a new app sets it in development), each SELECT carries the controller and action that sent it, which turns a cluster of reads into one line you can point at (same tag, repeated). In a test, wrap the request in ActiveSupport::Notifications.subscribed(..., "sql.active_record") and count: a page that was 2 queries this morning and is 11 queries now has a new lazy read a reader should name.

    From the lesson N+1 queries, eager loading and strict loading

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Fresher to mid

    A teammate's pull request keeps swapping includes(:teacher) for preload(:teacher) "to be explicit about the two SELECTs." Walk through when the swap is a no-op, when it is wrong, and what the SQL shape would be in each case.

    What it tests
    Reading the chain to decide between preload and eager_load, knowing when includes picks each one, and naming the exact behaviour of .references on a SQL fragment.
    First step
    Compare the three methods by what SQL they send. includes picks between preload (two SELECTs, no join) and eager_load (one SELECT with a LEFT OUTER JOIN) based on whether anything in the chain references the association's columns. preload always sends two SELECTs; eager_load always sends one SELECT with a join. From there walk through what each one does on the chain in question.
    Follow-ups

    From the lesson N+1 queries, eager loading and strict loading

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  2. Mid to senior

    An app with 400 pages wants N+1 regressions to stop at the model. How would you roll out strict_loading without surfacing errors to users, and when would you reach for :n_plus_one_only instead of the strict default?

    What it tests
    Choosing between the four places strict_loading lives (relation, record, association, environment), moving an app to it safely with action_on_strict_loading_violation, and knowing which lazy loads :n_plus_one_only still allows.
    First step
    Set config.active_record.strict_loading_by_default = true with action_on_strict_loading_violation = :log. The pair raises nothing on a user's page while every lazy load writes a line in the log the team can read. Fix the pages one by one, each one with an includes, a preload or an eager_load that matches the chain. Flip the setting to the default (:raise) only when the log is clean for a release.
    Follow-ups

    From the lesson N+1 queries, eager loading and strict loading

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  3. Senior to staff

    A teacher list reads teacher.batches.size thousands of times a minute, and a nightly import writes 40,000 rows with insert_all. Decide where size's numbers should come from: a counter cache, a cached SELECT, a materialised view, or a preloaded association, and say what each one costs when the import runs.

    What it tests
    Trading reads against writes with real numbers: how size picks length / count / counter_cache, which writers break which option, and when the fix belongs on the query instead of on the column.
    First step
    List every writer that touches batches, in order: user saves (go through callbacks), the import (insert_all skips callbacks), direct update_all (skips callbacks), and failed transactions (roll back). Then say what each option costs on each writer: a counter cache drifts when callbacks are skipped; a cached SELECT goes stale until expiry; a materialised view needs a refresh job; a preloaded association reads every row.
    Follow-ups

    From the lesson N+1 queries, eager loading and strict loading

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 5 minutes

All Ruby on Rails interview questions · The Ruby on Rails track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.