# Avoiding unnecessary queries in javascript/erb views

**URL:** <https://www.ruby-forum.com/t/avoiding-unnecessary-queries-in-javascript-erb-views/188045>\
**Category:** Rails\
**Created:** [May 4, 2010, 8:00pm UTC](https://www.ruby-forum.com/t/avoiding-unnecessary-queries-in-javascript-erb-views/188045 "2010-05-04T20:00:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 4, 2010, 8:00pm UTC](https://www.ruby-forum.com/t/avoiding-unnecessary-queries-in-javascript-erb-views/188045/1 "2010-05-04T20:00:24Z")

</div>

Hello

I have a js view, say, things/index.js.erb that looks like this:

$(‘my-stuff’).update("\<%= escape\_javascript(render :partial =\>  
‘shared/thing’, :collection =\> @things) %\>");  
Effect.toggle(‘my-stuff’, ‘slide’);

The idea is that the div with id ‘my-stuff’ will slide down and show  
the collection when a certain link is clicked, and it’ll slide up if  
clicked again. The problem is that on the second click, the file is  
parsed again by erb, which results in a query being made to the  
database to fetch the @things collection. This is unnecessry because  
the content is already displayed and is now being hidden from the  
user.

Is there any way to detect that situation and avoid that extra query?

Thanks,  
Andre
