Mark posts as solved in an importer

I’m working on an importer for answerhub and need to mark posts as solved. A couple other importers do something like this:

            if topic["answer"] == '[accepted]'
              post.custom_fields['is_accepted_answer'] = true

And then later call this:

  def mark_topics_as_solved
    puts "", "Marking topics as solved..."

    PostAction.exec_sql <<-SQL
      INSERT INTO topic_custom_fields (name, value, topic_id, created_at, updated_at)
      SELECT 'accepted_answer_post_id', pcf.post_id, p.topic_id, p.created_at, p.created_at
        FROM post_custom_fields pcf
        JOIN posts p ON = pcf.post_id
       WHERE = 'is_accepted_answer'

When I do that, the posts don’t show up as solved, but if I try to mark a post as solved, I get a 500 error (presumably because it’s trying to mark a solved post as solved).

Is there something that I need to do to get rails to notice that the table has been updated?

My other solution is to just do this as a post_create_action

            if topic["answer"] == '[accepted]'
              post.custom_fields['is_accepted_answer'] = true
              the_topic = Topic.find(parent)
              the_topic.custom_fields['accepted_answer_post_id'] =

Your other solution looks good. I hope you are calling action later.


Thanks, @vinothkannans… I wondered about that too, but it seems that is getting called, by the importer code. I see that stuff is in the stashers.

Well, post.custom_fields is getting updated, and the topic shows that the post is the solution, but the post itself is missing the solution marker.

It turns out that the appropriate value for “is_accepted_answer” is not true, but "true".



Thanks for sharing your solutions @pfaffman and @vinothkannans

I just re-used your code:

post.custom_fields["is_accepted_answer"] = "true"
topic.custom_fields["accepted_answer_post_id"] =!!

This worked to an extent, I can see the solved status when I scroll to the accepted post, but the import fell-short in two ways:

  1. Solved post is not quoted into 1st post of topic
  2. Title of solved post on index page is not prepended with a :ballot_box_with_check:

I am parsing through the code in plugin.rb but it’s a little beyond me… any ideas?


You need to mark topics as solved like it’s done in some of our import scripts.

1 Like

Running that once at the end of my import script worked perfectly! Thanks.