Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating counts in response to changes in middle of relation chain #125

Open
DavidMikeSimon opened this issue Apr 14, 2016 · 8 comments
Open

Comments

@DavidMikeSimon
Copy link

At the moment, it seems that counter_culture does not notice changes that happen on models in the middle of multi-level counter caches. For example, I added this code to the end of the "updates third-level counter cache on update" test, but it failed:

company2.industry_id = industry1.id
company2.save!

industry1.reload
industry2.reload

industry1.reviews_count.should == 69
industry2.reviews_count.should == 0
@magnusvk
Copy link
Owner

Yeah, I think you've hit on a real bug here. I don't think there's any handling on the models in the middle of a chain. Kind of surprising this hasn't come up before, actually. 😄

I can't promise I'll find the time to work on this anytime soon. But I'd be very happy to review a PR if you found the time.

Thanks for reporting this.

@QuotableWater7
Copy link

fwiw, we actually ran into this issue about 9 mo ago and resorted to a heavy-handed counting approach since I wasn't sure there was an easy way to handle the situation. If I find the time, I'll take a look at the code and see if I can figure out a reasonable approach

@SakthiGurunathan07
Copy link

Hi @magnusvk
Whether this issue is fixed or can u help us with some approach on how to fix this ?

Thanks,
Sakthi

@magnusvk
Copy link
Owner

magnusvk commented Jun 8, 2017

@SakthiGurunathan07 This is not fixed. Feel free to work on a PR here, I haven't had the time to work on a fix unfortunately.

@pedantic-git
Copy link

Oooof - just had a go at fixing this but it's hard. counter_culture tracks changes by inserting callbacks into the model that contains the counter_culture directive, but in order to do this, callbacks would have to be inserted into the intermediate models using reflection. I'm not sure it's easy to solve without a big rewrite of some of the core functionality. @magnusvk would you agree?

@magnusvk
Copy link
Owner

Yeah, unfortunately that sounds right.

@lukaszludzik
Copy link

This is not perfect solution, but works. If you want to count products for categories you must also count for subcategories.

class Product < ActiveRecord::Base
  belongs_to :sub_category
  counter_culture :sub_category
  counter_culture [:sub_category, :category]
end

class SubCategory < ActiveRecord::Base
  has_many :products
  belongs_to :category
  counter_culture :category, column_name: 'products_count', delta_magnitude: proc { 
    |sub_category| sub_category.products_count }
end

class Category < ActiveRecord::Base
  has_many :sub_categories
end

@sebastienhoorens
Copy link

We tried @lukaszludzik 's solution but it didn't work in our case. We will try to solve it using callbacks on the model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants