Replace a string in all posts

Topics.where(category_id: 123).each do |t|
  posts.where(topic_id: t).each do |p|
./launcher enter app
rails c
# paste in the stuff

You might make a backup first.


Thanks Jay, appreciate your time!

And I’ve learned the hard way to make a backup before I do anything serious :slight_smile:

update: It worked just great, but I had to use \n instead of /.

Also, it’s Topic and Post in the code above :slight_smile:

Good idea to do a rebake after running this as well. In my case, it was to regenerate thumbnails.


A post was merged into an existing topic: Replacing content in multiple topics with Regex?

Hey @nathank ! Curious if you found an upstream solution for this. We’re looking at a similar issue where we’d like to (essentially) automatically replace “X” with “Y” every time a user includes “X” in their topic or post.

That’s what the watched words feature does. It’ll replace strings when the post is created or edited.


1 Like

Thanks Jay! It looks like this will work for character-based sequences but not non-character formatting such as tab indents.

Use cases: we have some older writers on our platform who still treat a keyboard like a typewriter and hit the space bar 5 times for an indent. Other writers are copying and pasting with a tab indent. Both of these leave us with what discourse interprets as markup. (If I’m using the term correctly.)

Found a workaround on the theme component side where we got the css to essentially override the markup formatting, which we never need for our specific use case.

Cheers and thanks for your help!