# Mark posts as solved in an importer

**URL:** https://meta.discourse.org/t/mark-posts-as-solved-in-an-importer/80146
**Category:** Development
**Created:** [February 9, 2018, 12:23am UTC](https://meta.discourse.org/t/mark-posts-as-solved-in-an-importer/80146 "2018-02-09T00:23:07Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [February 9, 2018, 12:23am UTC](https://meta.discourse.org/t/mark-posts-as-solved-in-an-importer/80146/1 "2018-02-09T00:23:07Z")

</div>

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

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

```

And then later call this:

```ruby
  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 p.id = pcf.post_id
       WHERE pcf.name = 'is_accepted_answer'
    SQL
  end

```

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`

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

```

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [February 9, 2018, 6:38am UTC](https://meta.discourse.org/t/mark-posts-as-solved-in-an-importer/80146/2 "2018-02-09T06:38:47Z")

</div>

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

> <https://github.com/discourse/discourse-solved/blob/main/plugin.rb#L84-L87>

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [February 9, 2018, 12:57pm UTC](https://meta.discourse.org/t/mark-posts-as-solved-in-an-importer/80146/3 "2018-02-09T12:57:11Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [February 20, 2018, 8:05pm UTC](https://meta.discourse.org/t/mark-posts-as-solved-in-an-importer/80146/4 "2018-02-20T20:05:56Z")

</div>

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"`.

---

<div class="post-metadata">

### Author: ![andyh](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@andyh](https://meta.discourse.org/u/andyh)
#### Post date: [September 14, 2018, 7:28pm UTC](https://meta.discourse.org/t/mark-posts-as-solved-in-an-importer/80146/5 "2018-09-14T19:28:26Z")

</div>

👋

Thanks for sharing your solutions @pfaffman and @vinothkannans

I just re-used your code:

```plaintext
post.custom_fields["is_accepted_answer"] = "true"
topic.custom_fields["accepted_answer_post_id"] = post.id
topic.save!
post.save!

```

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 ☑

I am parsing through the code in [plugin.rb](https://github.com/discourse/discourse-solved/blob/master/plugin.rb) but it’s a little beyond me… any ideas?

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [September 14, 2018, 8:28pm UTC](https://meta.discourse.org/t/mark-posts-as-solved-in-an-importer/80146/6 "2018-09-14T20:28:46Z")

</div>

> [@andyh](#):
>
> Title of solved post on index page is not prepended with a ☑

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

> <https://github.com/discourse/discourse/blob/526ffc4966debbc3b48efc9896a23514f923cc9d/script/import_scripts/telligent.rb#L449-L459>

---

<div class="post-metadata">

### Author: ![andyh](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@andyh](https://meta.discourse.org/u/andyh)
#### Post date: [September 14, 2018, 8:44pm UTC](https://meta.discourse.org/t/mark-posts-as-solved-in-an-importer/80146/7 "2018-09-14T20:44:31Z")

</div>

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