tl;dr
For some posts, calling PostRevisor
sets the post_id
to nil
. Am I crazy?
Answer: No. PostRevisor
accesses post.topic
, which is nil
for a post in a deleted topic. And then it sets post.topic
to nil, which in turn sets post.topic_id to nil.
I think PostRevisor should get the topic like this:
@topic = topic || Topic.with_deleted.find_by(id: post_topic_id)
rather than like this:
@topic = topic
post=Post.find_by(topic_id: 179227, post_number: 12)
post.topic_id => 179227
pr=PostRevisor.new(post)
post.topic_id => nil
The Full Story
I’m working on a script that fixes goo.gl
links (the service is sunsetting soon, so the script finds goo.gl
links, fetches what they are redirected to and gsub
s the goo.gl
URL with the one it’s directed to. It mostly works.
But
For a bunch of posts, everything looks like it’s going just fine, but then PostRevisor
fails because post.acting_user
is nil. And then, in my rescue, it seems that the topic_id
is nil, but it’s not the post
that’s nil, because it still has a post_number
.
begin
puts "Revising (#{count}/#{total_posts}) https://mysite.com/t/#{post.topic_id}/#{post.post_number}"
puts "missing topic_id for post #{post.id}" if !post.topic_id
next if !post.topic_id
PostRevisor.new(post).revise!(system_user, raw: new_raw, **revision_options)
rescue => e
puts "cannot revise (number: #{count} https://tw.forumosa.com/t/#{post.topic_id}/#{post.post_number}): #{e}"
end
FIXING!!: https://goo.gl/maps/XaNG
B7qaZGzhBmM78 -----> https://www.google.com/maps/place/%E6%AD%A5%E9%81%93%E5%92%96%E5%95%A1%E9%A4%A8Cafe+Strada/@22.6300414,120.3153591,17z/d
ata=!3m1!4b1!4m5!3m4!1s0x346e04944a9b3471:0x520c1f01c3d62e57!8m2!3d22.6301115!4d120.3175543?shorturl=1
Revising (680/1773) https://mysite.com/t/207069/1817
cannot revise (number: 680 https://mysite.com/t//1817): undefined method `acting_user=' for nil
For the vast majority of posts, this works just fine, but for some subset of them, it fails like this. And if I run the code by hand line-by-line I get the same thing. It’s starting to look like, though, that if I do a pr=PostRevisor.new(post)
I see that the post in the pr record has no topic_id
and then if I inspect the post
it now has topic_id
set to nil.