Unusual behaviour accessing post values in rake task

This might come from a misunderstanding of how the database is interacted with via Ruby (ActiveRecord?) vs. Rails but I’m seeing an unusual error message while running a rake task.

The error message is repeated some number of times with different post IDs similar to this:

Failed to refresh post (topic_id: , post_id: 46367)

The error message is coming from this line in the rake task posts:refresh_emails:

Something common to every one of the posts that trigger this error is that the post is within a deleted topic. Failing to do something with it isn’t totally unexpected but what I find curious is that querying that post ID in rails, I can see that it does have a topic_id set which that puts line is failing to obtain.

[1] pry(main)> Post.where(id: 46367)
=> [#<Post:0x000055e95558ad10
  id: 46367,
  user_id: 3372,
  topic_id: 4225,
  ...
  raw_email:
   "<some valid raw email>",
  ...

I scanned the code a bit and the only obvious explanation I could find for an error being raised and printing that message is if it was also unable to obtain raw_email on line 290, which would cause Email::Receiver to raise during initialization.

That comes from being unfamiliar with Ruby and not burrowing particularly thoroughly into revise() though, there could easily be other places that might raise for other reasons.

Regardless, I’m a bit confused as to why it’s failing on these posts, it seems like this rake task should be able to operate on posts that happen to be part of deleted topics, including the first post. Especially given that a staff member might un-delete the topic without knowing that such a task even happened, let alone that it didn’t apply to that one topic.

In my case, I don’t need to do anything with these deleted topics and I know that they will not be un-deleted so I’m not looking for a solution, just wanted to bring it to someone’s attention. Maybe it’s something, maybe it’s nothing.