fromthepage icon indicating copy to clipboard operation
fromthepage copied to clipboard

Orphan article-article links cause 500 error

Open saracarl opened this issue 11 months ago • 2 comments

When going to this subject page: https://fromthepage.com/gri/stendahl-letters-qc/article/32081279 The customer gets the following 500 error:

I, [2024-03-20T17:55:01.594240 #11487]  INFO -- : Started GET "/gri/stendahl-letters-qc/article/32081279" for 136.62.254.224 at 2024-03-20 17:55:01 +0000
I, [2024-03-20T17:55:01.596616 #11487]  INFO -- : Processing by ArticleController#show as HTML
I, [2024-03-20T17:55:01.596898 #11487]  INFO -- :   Parameters: {"user_slug"=>"gri", "collection_id"=>"stendahl-letters-qc", "article_id"=>"32081279"}
I, [2024-03-20T17:55:01.746109 #11487]  INFO -- :   Rendered article/show.html.slim within layouts/application (Duration: 76.8ms | Allocations: 39797)
I, [2024-03-20T17:55:01.746930 #11487]  INFO -- :   Rendered layout layouts/application.html.slim (Duration: 77.7ms | Allocations: 40499)
I, [2024-03-20T17:55:01.748097 #11487]  INFO -- : Completed 500  in 151ms (ActiveRecord: 29.1ms | Allocations: 52682)
F, [2024-03-20T17:55:01.759191 #11487] FATAL -- : 
ActionView::Template::Error (undefined method `title' for nil:NilClass):
    19:     [email protected]_article_links.each do |link|
    20:       li
    21:         =svg_symbol '#icon-page', class: 'icon'
    22:         =link_to link.source_article.title, collection_article_show_path(@collection.owner, @collection, link.source_article.id)
    23:         ==" — #{link.display_text}"
  
app/views/article/_article_links.html.slim:22
app/views/article/_article_links.html.slim:19
app/views/article/show.html.slim:42
app/controllers/application_controller.rb:64:in `switch_locale'

Reported by Peyton at GRI.

saracarl avatar Mar 20 '24 17:03 saracarl

I think this is caused by source_article reference this line: https://github.com/benwbrum/fromthepage/blob/development/app/views/article/_article_links.html.slim#L22 I'm not sure where that is coming from; it's only referenced on this line. source_article has been there for 7 years, but perhaps last month's PR which addressed this issue on the number of subject counts on a page caused this?

saracarl avatar Mar 20 '24 18:03 saracarl

The problem happens when we are displaying article A, which lists all the articles that link to it. These are stored in article_article_links, so if article B links to A, there will be an ArticleArticleLink record with source_article_id: B, target_article_id: A.

The data shows that we have orphan articles. At some point, the system deleted article B, but did not delete the ArticleArticleLink object that linked B to A. As a result, when we display "what articles link here" for A, we see the link, try to display the title of B, but B is nil and we blow up.

I'm going to remediate the data. To figure out the root cause, we should test these scenarios:

  • UI Deletion
    • Create article A via upload or transcription.
    • Create article B
    • Edit the description on article B to link to A as link to [[A]]
    • Verify that displaying A and B work.
    • Delete article B
    • Check to see if viewing A blows up
  • On deduping
    • Create article A via upload or transcription.
    • Create article B
    • Edit the description on article B to link to A as link to [[A]]
    • Verify that displaying A and B work.
    • Create article B C
    • View article B C
    • Observe that article B is listed as a potential duplicate
    • Merge B into B C
    • Check to see if viewing A blows up.

To remediate the data:

source_article_ids = ArticleArticleLink.pluck(:source_article_id).uniq
article_ids = Article.pluck(:id)
orphan_source_article_ids = source_article_ids - article_ids
ArticleArticleLink.where(source_article_id: orphan_source_article_ids).delete_all

benwbrum avatar Mar 20 '24 19:03 benwbrum