The discussion revolves around programmatically adding likes to a post in Discourse after an import process failed to preserve them. piffy seeks a way to restore 100 likes without re-running the import script, which they are hesitant to do due to potential side effects. pfaffman suggests examining the import script’s logic, noting that likes are created via PostActionCreator and that no rake command exists for this purpose. piffy shares the relevant code from the import script and confirms they will use a similar approach in the Rails console. pfaffman offers a tip about using ; to avoid the less pager when running commands. Finally, piffy provides a working example of the console command to add a like, using PostActionCreator.create with the post ID, user, action type :like, and timestamp.
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?
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.
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.
Here’s the relevant code
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
...
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.)