# Cache output issue

**URL:** <https://www.ruby-forum.com/t/cache-output-issue/195098>\
**Category:** Rails\
**Created:** [September 16, 2010, 7:38pm UTC](https://www.ruby-forum.com/t/cache-output-issue/195098 "2010-09-16T19:38:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [September 16, 2010, 7:38pm UTC](https://www.ruby-forum.com/t/cache-output-issue/195098/1 "2010-09-16T19:38:17Z")

</div>

I am using memcached… and have the folllowing in my partial.

\<%CACHE.fetch ‘all\_categories’ do %\>

Category.all.each do |c|- \<%= c.name %\> 
end
\<%end %\>

When I check in the irb console the “all\_categories” key does get  
populated.  
So…CACHE.fetch ‘all\_categories’ in irb does output the expected html  
list.

But nothing gets outputted in my view.

I am guessing this is some sort of syntax error, what am I doing wrong?

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [September 16, 2010, 8:43pm UTC](https://www.ruby-forum.com/t/cache-output-issue/195098/2 "2010-09-16T20:43:52Z")

</div>

Not sure what is “Category.all” in your “each” enumerator.

Try this, assuming CACHE.featch ‘all\_categories’ is returning an array  
of  
sorts:

\<% CACHE.fetch(‘all\_categories’).each do |category| %\>

- \<%= category.name %\> 

\<% end %\>

On Thu, Sep 16, 2010 at 11:02 PM, badnaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When I check in the irb console the “all\_categories” key does get  
> “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Srikanth S.  
[http://www.srikanthps.com](http://www.srikanthps.com)  
@srikanthps

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [September 17, 2010, 1:09am UTC](https://www.ruby-forum.com/t/cache-output-issue/195098/3 "2010-09-17T01:09:01Z")

</div>

On Sep 16, 6:32 pm, badnaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When I check in the irb console the “all\_categories” key does get  
> populated.  
> So…CACHE.fetch ‘all\_categories’ in irb does output the expected html  
> list.
> 
> But nothing gets outputted in my view.
> 
> I am guessing this is some sort of syntax error, what am I doing wrong?

In the case where the data is already in the cache the block is not  
yielded to, so nothing will be output. You could try changing it to a \<  
%= but I’m not sure that mixes well with yielding to a block or the  
case when it is generated fresh data. Why not use cache\_fragment ?

Fred

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [September 17, 2010, 11:57pm UTC](https://www.ruby-forum.com/t/cache-output-issue/195098/4 "2010-09-17T23:57:41Z")

</div>

On Sep 17, 8:09 pm, badnaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The it does work, BUT…somehow the page url is prepended to the cache  
> key so essentially this fragment which is same across the app, misses  
> for every new page load.

cache fragments are keyed like that by default - you can override  
that by passing :controller/:action parameters to make up a path (this  
doesn’t need to be a real path in your application - it’s just how it  
builds the identifier for the cache entry

Fred

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [September 17, 2010, 9:10pm UTC](https://www.ruby-forum.com/t/cache-output-issue/195098/5 "2010-09-17T21:10:51Z")

</div>

You are right…

changing it to the following throws a nasty syntax error.

\<%= Rails.cache.fetch “all\_catgory” do %\>

\<% end %\>

If I do

\<% cache(:key =\> ‘all\_category’) do %\>

\<%end%\>

The it does work, BUT…somehow the page url is prepended to the cache  
key so essentially this fragment which is same across the app, misses  
for every new page load.

[DEBUG 17-09-2010 12:06:37] Cached fragment hit: views/localhost/  
categories/autos-motorcyles/vote\_topics?key=all\_category (0.4ms)  
[DEBUG 17-09-2010 12:06:37] Category Load (0.2ms) SELECT \* FROM  
`categories` ORDER BY name ASC  
[DEBUG 17-09-2010 12:06:37] Cached fragment miss: views/localhost/  
categories/autos-motorcyles/vote\_topics?key=all\_category (0.4ms)  
[DEBUG 17-09-2010 12:06:37] Rendered shared/\_cat\_nav\_list (8.9ms)  
[DEBUG 17-09-2010 12:06:37] Cached fragment hit: views/localhost/  
categories/autos-motorcyles/vote\_topics?key=all\_cities (0.3ms)  
[DEBUG 17-09-2010 12:06:37] User Load (0.1ms) SELECT distinct city  
FROM `users`  
[DEBUG 17-09-2010 12:06:37] Cached fragment miss: views/localhost/  
categories/autos-motorcyles/vote\_topics?key=all\_cities (0.4ms)  
[DEBUG 17-09-2010 12:06:37] Rendered shared/\_city\_nav\_list (4.7ms)

Can someone please help!

On Sep 16, 4:07 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
