# Add likes programmatically?

**URL:** https://meta.discourse.org/t/add-likes-programmatically/242993
**Category:** Support
**Created:** [October 26, 2022, 6:24pm UTC](https://meta.discourse.org/t/add-likes-programmatically/242993 "2022-10-26T18:24:50Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![piffy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/piffy/32/254198_2.png) [@piffy](https://meta.discourse.org/u/piffy)
#### Post date: [October 26, 2022, 6:24pm UTC](https://meta.discourse.org/t/add-likes-programmatically/242993/1 "2022-10-26T18:24:51Z")

</div>

During import to discourse, a major post lost 100 likes in the transition (I failed to scrape it properly). I prefer not to re-run the import script in this case.

If I have the list of users who liked the post, what’s the best way of adding those? Is there a rake command?

---

<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: [October 26, 2022, 7:39pm UTC](https://meta.discourse.org/t/add-likes-programmatically/242993/2 "2022-10-26T19:39:58Z")

</div>

What I’d do is look at the import script that imports the likes to see what it does to add the likes then do that in the rails console. I’m fairly certain it’s a `PostAction`.

I’m fairly certain that there is not such a rake command, as it’s a fairly unusual request (mostly, I’d just re-run the import script), but I haven’t looked.

---

<div class="post-metadata">

### Author: ![piffy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/piffy/32/254198_2.png) [@piffy](https://meta.discourse.org/u/piffy)
#### Post date: [October 26, 2022, 7:56pm UTC](https://meta.discourse.org/t/add-likes-programmatically/242993/3 "2022-10-26T19:56:16Z")

</div>

Thanks for the response. I am nervous to re-run the import script since it’s been almost 6 months and I’m not sure what kind of side-effects it could have in edge cases where old data interacts with new data.

Here’s the helper function I used in my import script.

> <https://github.com/discourse/discourse/blob/fcc2e7ebbf3fa5f17c24c70149a949850f29a5f3/script/import_scripts/base.rb#L643>

Here’s the relevant code

```plaintext
created_by = User.find_by(id: user_id_from_imported_user_id(params[:user_id]))
post = Post.find_by(id: post_id_from_imported_post_id(params[:post_id]))

if created_by && post
          PostActionCreator.create(created_by, post, :like, created_at: params[:created_at])
          created += 1
...

```

I will run something similar to this. Thanks!

---

<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: [October 26, 2022, 8:11pm UTC](https://meta.discourse.org/t/add-likes-programmatically/242993/4 "2022-10-26T20:11:07Z")

</div>

> [@piffy](#):
>
> I am nervous to re-run the import script since it’s been almost 6 months

I would be quite nervous as well!

> [@piffy](#):
>
> I will run something similar to this. Thanks!

Yippee! You never know when providing an answer as vague as “Well, what I would do. . .” whether it’ll be of any use.

If you’re going to paste stuff into the rails console, a handy trick to know is that if you terminate your lines with a `;` you won’t get pushed into a `less` pager to see the results of the last statement. (If you write a proper loop, or run a script you don’t need this tip.)

Good luck!

---

<div class="post-metadata">

### Author: ![piffy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/piffy/32/254198_2.png) [@piffy](https://meta.discourse.org/u/piffy)
#### Post date: [November 14, 2022, 10:27pm UTC](https://meta.discourse.org/t/add-likes-programmatically/242993/5 "2022-11-14T22:27:01Z")

</div>

If anyone wants to do this, here’s what worked for me. Open the rails console and run:

```plaintext
p = Post.find(31871)
u = User.find_by(username:"xxx")
PostActionCreator.create(u, p, :like, created_at: Time.now)

```

Obviously you will have to swap out the post id and the username

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [December 14, 2022, 10:27pm UTC](https://meta.discourse.org/t/add-likes-programmatically/242993/6 "2022-12-14T22:27:45Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
