Challenge - 5 Problems
Fragment Caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this fragment cache block output?
Consider this Rails view snippet using fragment caching:
What will be rendered on the page the first time and on subsequent requests?
<% cache('user_42_profile') do %>
<p>User profile content</p>
<% end %>What will be rendered on the page the first time and on subsequent requests?
Ruby on Rails
<% cache('user_42_profile') do %>
<p>User profile content</p>
<% end %>Attempts:
2 left
💡 Hint
Think about how fragment caching stores rendered HTML to speed up future requests.
✗ Incorrect
Fragment caching stores the rendered HTML the first time the block runs. Later requests use the cached HTML directly, skipping the block.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this fragment cache usage
Which option contains a syntax error in using fragment caching in Rails views?
Attempts:
2 left
💡 Hint
Look for missing 'do' or incorrect ERB tags.
✗ Incorrect
Option B is missing the 'do' keyword and has incorrect ERB tag placement, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this fragment cache not update after model change?
Given this code snippet:
After updating @post.title in the database, the cached fragment still shows the old title. Why?
<% cache(@post) do %> <p><%= @post.title %></p> <% end %>
After updating @post.title in the database, the cached fragment still shows the old title. Why?
Ruby on Rails
<% cache(@post) do %> <p><%= @post.title %></p> <% end %>
Attempts:
2 left
💡 Hint
Check how Rails generates cache keys for Active Record objects.
✗ Incorrect
Rails uses the model's cache_key, which includes timestamp. If the record was not saved or timestamp not updated, cache key stays same, so cache is reused.
🧠 Conceptual
advanced2:00remaining
What is the purpose of cache digests in fragment caching?
In Rails fragment caching, what role do cache digests play?
Attempts:
2 left
💡 Hint
Think about how Rails knows when to expire cached fragments if partial templates change.
✗ Incorrect
Cache digests generate fingerprints of partial templates. When partials change, digests change, causing cache expiration automatically.
❓ state_output
expert2:00remaining
How many cached fragments exist after this code runs?
Given this Rails view code:
After rendering once, how many separate cached fragments are stored?
<% ['a', 'b', 'c'].each do |letter| %>
<% cache("fragment_#{letter}") do %>
<p>Letter: <%= letter %></p>
<% end %>
<% end %>After rendering once, how many separate cached fragments are stored?
Ruby on Rails
<% ['a', 'b', 'c'].each do |letter| %> <% cache("fragment_#{letter}") do %> <p>Letter: <%= letter %></p> <% end %> <% end %>
Attempts:
2 left
💡 Hint
Each cache block with a unique key creates one fragment cache.
✗ Incorrect
Each iteration caches a separate fragment with a unique key, so 3 fragments total.