V
V
vadimstroganov2016-07-15 17:33:04
Ruby on Rails
vadimstroganov, 2016-07-15 17:33:04

Is it possible to access the current object in an if statement?

last_update = Page.order(updated_at: :desc).first.updated_at.to_i
if Rails.cache.fetch("#{last_update}/all_pages_cached").present?
  Rails.cache.fetch("#{last_update}/all_pages_cached")
else
  Rails.cache.fetch("#{last_update}/all_pages_cached") do
    hash # некий хэш
  end
end

Is it possible to access the current object ?
For example, to write like this:
last_update = Page.order(updated_at: :desc).first.updated_at.to_i
if Rails.cache.fetch("#{last_update}/all_pages_cached").present?
  current_object # вот так
else
  Rails.cache.fetch("#{last_update}/all_pages_cached") do
    hash # некий хэш
  end
end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@mgyk, 2016-07-15
_

If there is no such data in the cache (a cache miss), then nil will be returned. However, if a block has been passed, that block will be passed the key and executed in the event of a cache miss. The return value of the block will be written to the cache under the given cache key, and that return value will be returned.

Rails.cache.fetch("#{last_update}/all_pages_cached") do
   hash
end

If there is no value in the cache, then the block is executed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question