impressionist icon indicating copy to clipboard operation
impressionist copied to clipboard

Counter Cache when using Slug

Open Ryan-Berry opened this issue 11 years ago • 1 comments

Hi,

Love what you've done with this gem but am having problems with the counter_cache when using slugged URLS.

I got around this issue as other here have in my controller but grabbing my instance like so: @event = Event.find_by_slug!(params[:id]) and then in the same controller action I can simply add impressionist(@event, "Page view", :unique => [:session_hash]) as your docs suggest. That works fine.

When I add the counter cache column to my Event model I get the following error:

String can't be coerced into Fixnum vendor/bundle/ruby/1.9.1/gems/impressionist-1.5.1/lib/impressionist/update_counters.rb:22:in `-'

Which if my understanding is correct is because it's viewing the slugged url as if it should be an id. Can I call the counter_cache from anywhere other than the model? Currently I have it as the docs suggest: is_impressionable :counter_cache => true.

Oddly, this seemed to work ok in development - it's only when I push to heroku that it throws this error!?

Would appreciate some help here.

Thanks Ryan

Ryan-Berry avatar Apr 25 '14 11:04 Ryan-Berry

We've had the same error when using Mongoid for an impressions_field column that we hadn't cast to an integer and then used something like rails_admin to view the object, with as result that the impressions_field got cast to a String on save. Double check that your impressions_field on your object isn't a String, and if it is, you can fix it by saving it as an Integer, and then making sure that the impressions_field has type Integer.

simonv3 avatar Jan 04 '16 01:01 simonv3