요약
일부 게시글에서 PostRevisor를 호출하면 post_id가 nil로 설정됩니다. 제가 이상한 건가요?
답: 아닙니다. PostRevisor는 post.topic에 접근하는데, 삭제된 토픽에 있는 게시글의 경우 이는 nil입니다. 그리고 나서 post.topic을 nil로 설정하게 되고, 이는 다시 post.topic_id를 nil로 설정하게 됩니다.
PostRevisor는 다음과 같이 토픽을 가져와야 한다고 생각합니다:
@topic = topic || Topic.with_deleted.find_by(id: post_topic_id)
다음과 같이 가져오는 대신 말입니다:
@topic = topic
post=Post.find_by(topic_id: 179227, post_number: 12)
post.topic_id => 179227
pr=PostRevisor.new(post)
post.topic_id => nil
전체 이야기
goo.gl 링크를 수정하는 스크립트를 작업 중입니다(해당 서비스가 곧 종료될 예정이므로, 스크립트는 goo.gl 링크를 찾아 리디렉션되는 대상 URL을 가져오고, gsub로 goo.gl URL을 해당 대상 URL로 대체합니다. 대부분 잘 작동합니다).
그러나
많은 게시글에서 모든 것이 정상적으로 진행되는 것처럼 보이다가, post.acting_user가 nil이기 때문에 PostRevisor가 실패합니다. 그리고 나서, 제 rescue 블록에서 topic_id가 nil인 것으로 보이지만, post 자체는 nil이 아닙니다. 왜냐하면 여전히 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
대부분의 게시글에서는 이 방식이 잘 작동하지만, 일부 하위 집합에서는 이와 같이 실패합니다. 그리고 코드를 수동으로 한 줄씩 실행해도 동일한 결과를 얻습니다. 하지만, pr=PostRevisor.new(post)를 실행하면 pr 레코드에 있는 post에 topic_id가 없고, 그 후 post를 검사하면 이제 topic_id가 nil로 설정되어 있는 것으로 보입니다.