5

I have a Rails model for a Recipe. It has an attribute, views, which counts how many times it has been viewed.

In the show action of my controller, I fetch the model object normally:

@recipe = Recipe.find(params[:id])

Then I increase the view count and save:

@recipe.views = @recipe.views + 1
@recipe.save

This works without a hitch locally, but on Heroku, the save apparently doesn't happen. No errors are thrown.

I can run this exact same code in the rails console on Heroku, and then it works.

The only way I can get it to work, is setting

config.cache_classes = false

in environmenst/production.rb

This is obvously not what I want, but I'm stumped about how to go from here.

I'm running Rails 3.2.8, Ruby 1.9.3 on the Cedar stack, using Postgresql 9.1 in both development and on production.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Azzar
  • 51
  • 3
  • Just to clean up the code, you could use `@recipe.increment!(:views)` for your counter (also the ! bang will throw any errors). What db are you using? – veritas1 Sep 06 '12 at 11:35
  • It's postgresql 9.1 both locally and on Heroku. I have a strong feeling it's some sort of caching problem (the action not even being hit on Heroku). Still, I wonder why cache_classes = false makes it work. If I set it to back to true, save stops working. – Azzar Sep 06 '12 at 11:39
  • No errors are thrown, but do you see any hints in the logs? – agmcleod Sep 06 '12 at 12:43
  • Why not use the impressionist gem, its much easier https://github.com/charlotte-ruby/impressionist and an example http://stackoverflow.com/questions/4815713/simple-hit-counter-for-page-views-in-rails – Hishalv Sep 06 '12 at 15:12
  • +1 for admitting the mistake :) – Taryn East Sep 12 '12 at 01:25
  • @Azzar, please consider entering your solution as an answer and then marking it as the answer. That way this will not show as an unresolved question. – Tass Oct 03 '12 at 16:18
  • +1 Tass, Please mark this as closed. – Eric Fode Feb 26 '13 at 22:14
  • @Azzar - If you're game to recap the solution as your own answer, I'll delete my answer. (See http://meta.stackexchange.com/questions/90263/unanswered-question-answered-in-comments for elaboration of why this is helpful.) Thanks! – DreadPirateShawn Oct 09 '13 at 16:26
  • Thank you for your "mistake". I view this post and realized I had the same issue and it saves me potentially many hours of banging my head on the wall.. ha – Antonio Jha Apr 07 '15 at 10:45

2 Answers2

6

FWIW to future searchers looking for their own solution, Benjamin Tan's answer (in comments) of heroku restart was what worked for me when I had a similar problem.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Tony Zito
  • 534
  • 6
  • 11
0

Copying the answer from the edited question body in order to remove this question from the "Unanswered" filter:

UPDATE: Fixed

Turns out I had a file with another older definition of the controller in the app/controllers directory.

~ answer per Azzar

Cœur
  • 37,241
  • 25
  • 195
  • 267
DreadPirateShawn
  • 8,164
  • 4
  • 49
  • 71