fromthepage
fromthepage copied to clipboard
Page count for subject out of sync after deduplication merge
Jon at WWP reports that subject page counts are off after a deduplication merge. This appears to be a result of the Rails counter cache call-back not being called when we update the page_article_links
in the article text rename/page save phase of article combining.
{"id"=>89811,
"title"=>"Alexander Badlam, b. 1808",
"source_text"=>"",
"created_on"=>Tue, 02 Feb 2021 00:59:38.000000000 UTC +00:00,
"lock_version"=>1,
"xml_text"=>
"<?xml version='1.0' encoding='UTF-8'?> \n" +
" <page>\n" +
" <p/>\n" +
" </page>\n",
"graph_image"=>
"/home/fromthepage/deployment/releases/20240325204529/public/images/working/dot/89811.png",
"collection_id"=>970,
"latitude"=>nil,
"longitude"=>nil,
"uri"=>"",
"provenance"=>nil,
"created_by_id"=>221669,
"pages_count"=>126}
=> 0
2.7.3 :028 > to_article.pages.count
=> 172
2.7.3 :029 > to_article.pages.uniq.count
=> 143
2.7.3 :030 > to_article.pages_count
=> 126
Getty also reports that This also is a problem when someone is updating a link on page: subject A is linked withing a page transcription user updates that to instead link to subject B then both A and B will have incorrect page counts.
We suspect if we figure out what's going on there, the dedupe problem outlined above will also be fixed. Both seem to be in the page save callback. (We thought that should be automated, but we're not sure how that works with had_and_belongs_to_many; we haven't used counter caches a lot.)
Seems like the counter_cache is bugged with has_many :through relationships. I would say the approach should be a callback to the article model itself to update the counts, OR drop the pages_count for now although that might cause some performance hit
See issue https://discuss.rubyonrails.org/t/counter-cache-for-has-many-through-associations/70118/2
Do you think that this bug has been fixed in newer versions of Rails? If so, I'd prefer to put our effort into upgrading our framework rather than doing work-arounds here.
I tried and confirmed this behaviour is working correctly in ruby 3.3.3, and it works with update callbacks as well.