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.
Warm-up questions
Short questions with short answers, each answered in full.
-
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. Withconfig.active_record.query_log_tags_enabledon (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 inActiveSupport::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.
-
Fresher to mid
A teammate's pull request keeps swapping
includes(:teacher)forpreload(: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
.referenceson 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
-
- When does includes switch from preload to eager_load, and how does
.references(:assoc)enter that decision? - A page filters by
teachers.name; which of the three forms will error, and why? - A has_many eager_load returns 50 rows from 10 parents. How many parents end up in the result, and what does
.distinctdo here? - How would a query-budget test tell the two shapes apart without reading the SQL?
- When does includes switch from preload to eager_load, and how does
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.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
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
-
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_onlyinstead 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_onlystill allows. - First step
- Set
config.active_record.strict_loading_by_default = truewithaction_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
-
- What does
:n_plus_one_onlyallow, and which read in the recorded run still raised under it? - A has_many on a model is set
strict_loading: true. Which lazy reads from the parent now raise? - How would you keep an admin tool (one-off page, no latency budget) exempt from strict_loading while production is strict by default?
- Why is a query-budget test still useful when strict_loading is on by default?
- What does
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
-
Senior to staff
A teacher list reads
teacher.batches.sizethousands of times a minute, and a nightly import writes 40,000 rows withinsert_all. Decide wheresize'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
sizepicks 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_allskips callbacks), directupdate_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
-
- When does
.sizeon an association send SQL, when does it read a loaded array, and when does it read the counter column? - How would you repair the counter cache after the import, without a long table lock?
- A page shows the top 10 teachers by
batches.size. Which option scales there, and which one does not? - When is dropping the column and reading the count in the query the right call?
- When does
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