0
0
Ruby on Railsframework~20 mins

Fragment caching in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fragment Caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this fragment cache block output?
Consider this Rails view snippet using fragment caching:
<% 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 %>
AFirst request renders the content and caches it; later requests render the cached content without re-executing the block.
BEvery request renders the content and updates the cache with new content.
CThe cache block is ignored and content is always rendered fresh.
DThe cache block raises an error because the key is a string.
Attempts:
2 left
💡 Hint
Think about how fragment caching stores rendered HTML to speed up future requests.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this fragment cache usage
Which option contains a syntax error in using fragment caching in Rails views?
A<% cache(user) do %>Content<% end %>
B<% cache 'user' Content <% end %>
C<% cache 'user' do %>Content<% end %>
D<% cache user do %>Content<% end %>
Attempts:
2 left
💡 Hint
Look for missing 'do' or incorrect ERB tags.
🔧 Debug
advanced
2:00remaining
Why does this fragment cache not update after model change?
Given this code snippet:
<% 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 %>
AFragment caching does not work with instance variables.
BRails caches the fragment forever by default, ignoring model changes.
CThe cache block must be manually expired after updating the model.
DThe cache key is based on @post's cache_key method, which did not change because the record was not saved properly.
Attempts:
2 left
💡 Hint
Check how Rails generates cache keys for Active Record objects.
🧠 Conceptual
advanced
2:00remaining
What is the purpose of cache digests in fragment caching?
In Rails fragment caching, what role do cache digests play?
AThey compress cached fragments to save space.
BThey encrypt cached fragments for security.
CThey track dependencies of partials to expire caches automatically when partials change.
DThey store user session data inside cached fragments.
Attempts:
2 left
💡 Hint
Think about how Rails knows when to expire cached fragments if partial templates change.
state_output
expert
2:00remaining
How many cached fragments exist after this code runs?
Given this Rails view code:
<% ['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 %>
A3 cached fragments, one for each letter 'a', 'b', and 'c'.
B1 cached fragment containing all letters combined.
CNo cached fragments because cache keys are dynamic strings.
D6 cached fragments due to internal Rails caching of partials.
Attempts:
2 left
💡 Hint
Each cache block with a unique key creates one fragment cache.