magnusvk/counter_culture

counter_culture_fix_counts not working on model with default_scope defined

Closed this issue · 2 comments

I have two models:

Class User
  default_scope {includes(:location)}

  has_many :sleeps

  # major_sleeps_count
...
end

Class Sleep
  belongs_to :user
  counter_culture :user,
                  column_name: proc { |model| model.major_sleep.present? ? 'major_sleeps_count' : nil },
                  column_names: { ["sleeps.major_sleep IS NOT NULL"] => 'major_sleeps_count' }
  ...
end

When running Sleep.counter_culture_fix_counts I'm getting the error:

ActiveModel::MissingAttributeError (missing attribute: location_id)

So I have to admit I don't 100% understand why this is getting raised, but adding an includes on a default scope is almost certainly a bad idea. I would advise you to drop that and add a scope instead that you can use everywhere you want to add that includes.

Oh, I totally agree, but sadly that is not easily done in my case since it's an existing application