I have a model which is an image gallery. I want to keep track of how many views the image gets. Currently I achieve this in the controller
def show
@article = Post.find(params[:id])
@article.views += 1
@article.save
end
However, this only increments the views counter when accessing the full view (and this method also prevents me from caching the template). I also want to increment views when thumbnails are presented in a index collection view (because those are valid impressions as well), as well as searches, in other presentations of "likes", "favorites" etc.
In other words, any time the full image or thumb of the image is shown, I want to increment the views counter. Is there any pattern/paradigm for achieving this in rails?
Should I do this in javascript, such that after the page loads, I get a list of every post id on the page then ajaxly hit another controller which increments the views counter for all those posts?