Counter Cache when using STI throws: You have a nil object when you didn't expect it! #18

Closed
patroza opened this Issue Dec 20, 2011 · 3 comments

Comments

Projects
None yet
4 participants
@patroza

patroza commented Dec 20, 2011

The model uses Single Table Inheritance. If the type column is blank, the issue does not occur.

In model:
is_impressionable :counter_cache => { :column_name => :view_counter_cache }

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

Backtrace:
impressionist (1.0.1) app/models/impressionist/impressionable.rb:47:in update_counter_cache' activesupport (3.1.3) lib/active_support/core_ext/object/try.rb:32:intry'
impressionist (1.0.1) app/models/impression.rb:13:in update_impressions_counter_cache' activesupport (3.1.3) lib/active_support/callbacks.rb:401:in_run_save_callbacks'
activesupport (3.1.3) lib/active_support/callbacks.rb:81:in run_callbacks' activerecord (3.1.3) lib/active_record/callbacks.rb:264:increate_or_update'
activerecord (3.1.3) lib/active_record/persistence.rb:37:in save' activerecord (3.1.3) lib/active_record/validations.rb:50:insave'
activerecord (3.1.3) lib/active_record/attribute_methods/dirty.rb:22:in `save'

The problem seems to stem from using self.class.counter_cache_options

@patroza

This comment has been minimized.

Show comment Hide comment
@patroza

patroza Dec 20, 2011

I have worked around the issue by specifying the is_impressionable :counter_cache => .... in each STI model subclass.
Preferably this would not be needed :)

patroza commented Dec 20, 2011

I have worked around the issue by specifying the is_impressionable :counter_cache => .... in each STI model subclass.
Preferably this would not be needed :)

@coryschires

This comment has been minimized.

Show comment Hide comment
@coryschires

coryschires Dec 20, 2011

Contributor

I agree, you should not need to re-specify in the subclasses. I'll have a look at it over the next few days to see if we can make this smarter.

Contributor

coryschires commented Dec 20, 2011

I agree, you should not need to re-specify in the subclasses. I'll have a look at it over the next few days to see if we can make this smarter.

@ghost ghost assigned coryschires May 8, 2013

@acnalesso

This comment has been minimized.

Show comment Hide comment
@acnalesso

acnalesso May 8, 2013

Member

Has this issue been fixed? can we close it?

Member

acnalesso commented May 8, 2013

Has this issue been fixed? can we close it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment