I am using Ruby on Rails v3.2.2 and I would like to retrieve database data by disabling the system cache only in a case. That is, in my view file I have something like the following:
<h1>Random articles 1</h1>
<%= Article.order('RAND()').limit(3).inspect %>
...
<h1>Random articles 2</h1>
<%= Article.order('RAND()').limit(3).inspect %>
When the view file is rendered it outputs the same data for both under “Random articles 1” and “Random articles 2”. It happens because the Ruby on Rails cache system (by “default”/”convention”) tries to hit the database as less as possible for performance reasons.
How can I prevent this behavior (just for the above explained case) so to output different data for finder methods in my view file?
There is
uncachedmethod in ActiveRecord. Looks like you can use it like that:but you better extract that into class method of your model.
See this article for more information