0
0
Ruby on Railsframework~10 mins

Why caching improves response times in Ruby on Rails - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to cache the result of a method call in Rails.

Ruby on Rails
Rails.cache.[1]('user_1') { User.find(1) }
Drag options to blanks, or click blank then click option'
Afetch
Bstore
Cread
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'read' only reads without storing new data.
Using 'store' requires a value, not a block.
2fill in blank
medium

Complete the code to expire a cached fragment in Rails.

Ruby on Rails
expire_fragment([1])
Drag options to blanks, or click blank then click option'
Acache_key_for_users
BUser.all
C:all_users
D'all_users'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an ActiveRecord relation instead of a cache key string.
Using a symbol instead of a string key.
3fill in blank
hard

Fix the error in the caching code to properly cache a view fragment.

Ruby on Rails
<% cache [1] do %>
  <p>Cached content here</p>
<% end %>
Drag options to blanks, or click blank then click option'
AUser.all
B'user_list'
C:user_list
Duser_list
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable without quotes causing undefined variable error.
Using a symbol instead of a string key.
4fill in blank
hard

Fill both blanks to create a cache key with a timestamp to expire cache after update.

Ruby on Rails
cache_key = "users/[1]-#{User.maximum(:[2])}"
Drag options to blanks, or click blank then click option'
Alist
Bupdated_at
Ccreated_at
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'created_at' which doesn't change on updates.
Using 'count' which doesn't reflect update time.
5fill in blank
hard

Fill all three blanks to write a method that caches a user's profile with expiration.

Ruby on Rails
def cached_profile(user)
  Rails.cache.[1]("profile_#{user.[2]", expires_in: [3]) do
    user.profile_data
  end
end
Drag options to blanks, or click blank then click option'
Afetch
Bid
C5.minutes
Dstore
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'store' which requires a value, not a block.
Using 'name' instead of 'id' for cache key.
Not setting expiration time.