0
votes

Anyone know why I get a syntax error when I try to use curly braces block syntax for Rails.cache.fetch

This errors out with error (syntax error, unexpected '{', expecting keyword_end (SyntaxError) )

Rails.cache.fetch "person/#{id}" { find(id) }

The following works:

Rails.cache.fetch "person/#{id}" do
  find(id)
end
2

2 Answers

0
votes

the short block needs () before to recognise the syntax it should look something like

Rails.cache.fetch("person/#{id}") { find(id) }
0
votes

This is because {} has different precedence than do end. In the first expression they are tied to the "person/#{id}" part of the expression.

So your first example is similar to:

Rails.cache.fetch("person/#{id}" { find(id) })

while the second is:

Rails.cache.fetch("person/#{id}") { find(id) }