How to replace a string/character in topic title?

AI-generated summary

The discussion revolves around replacing a string/character in a topic title. Pad_Pors wants to replace all - in topic titles with a single space using a rake command. However, this command affects all posts, not just topic titles.

Pad_Pors attempts to use regular expressions, but xrav3nz points out that rake post:remap does not change the title. zogstrip suggests using the rails console to update the title, but the initial attempt is unsuccessful.

After multiple attempts and suggestions from zogstrip and pfaffman, Pad_Pors finally resolves the issue by using the following code in the rails console:

Topic.where("title LIKE '% %'").find_each do |topic|
  topic.title.gsub!(/-/," ")
  topic.fancy_title.gsub!(/-/," ")
  topic.slug.gsub!(/-/," ")
  puts topic.title
  begin
    topic.save!
  rescue
    puts "oops, something broke."
  end
end

This code replaces - with a space in the title, fancy title, and slug.

sam points out that the correct efficient way to do this was initially suggested by zogstrip, but the discussion had become confusing. To run the code, one needs to enter the rails console using sudo ./launcher enter app and then rails c.

I want to replace all the - in the topic title with a single space.

this command will do it for all the posts, how can I change it so that only a topic-title get affected?

rake posts:remap["-"," "]

As mentioned before, use regular expressions :slightly_smiling_face:

the regular expression for title should be something like this:
<title>()</title>

how can I search in title tags in the remap command?

rake posts:remap[“-”," "]

rake post:remap does not change the title
 I am not sure if any of the rake tasks does this.

You are probably better off with a one time custom Ruby script.

That’s not possible using the remap rake task.

You’ll have to use the rails console to do that. Something like this

rails c
Topic.exec_sql("UPDATE topics SET title = replace(title, '-', ' '), fancy_title = NULL, slug = NULL WHERE title <> replace(title, '-', ' ')")

thanks, but this command didn’t replaced the “-” in titles with space.

any other suggestion would be appreciated.

I’ve edited my query, can you try again?

thanks, I tried it. the output is:

#<PG::Result:0x00007feb4faf0298 status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=6518>

but the titles haven’t changed.

Forgot to clear both fancy_title and slug columns. Query updated :arrow_up:

#<PG::Result:0x00007fc60eab0ca8 status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=0>

no change has been made to titles :face_with_raised_eyebrow:

update: slug is set to encoded in our case, should I change it in the command? @zogstrip

Here’s my try. It’s not tested, so please backup first!

I don’t know how this interacts with “encoded”. Perhaps that means that dashes are encoded as something other than a -. If that’s the case, you’ll need to modify things accordingly.

Topic.where("title LIKE '%-%'").find_each do |topic|
  topic.title.gsub!(/-/," ")
  topic.fancy_title.gsub!(/-/," ")
  topic.slug.gsub!(/-/," ")
  puts topic.title
  begin
    topic.save!
  rescue
    puts "oops, something broke."
  end
end

thanks, it gave back nil ,

just a double check to be sure I’m doing it right; I enter the app, then run the command in the rails console.

sudo ./launcher enter app
rails c

If you got nil, then it didn’t find anything to replace. You need to see just what a - is encoded as and do that replacement.

You might do a

Topic.where("title LIKE '%A PHRASE FROM A TOPIC YOU KNOW NEEDS FIXING%')

and you can have a look at what’s in the title field. If there are lots of them, you type q to get back to the prompt.

it seems that - is encoded as space in the title!

when I search for topics with space, it found me titles with space replaced with -.

this way is there any chance to update titles using a query?

p.s.: I hope I could explain it clearly.

Then perhaps they’re fixed already or you need to reload?

thanks @pfaffman , @zogstrip

it has been solved as suggested, and I only needed to put space in the find code:

Topic.where("title LIKE '% %'").find_each do |topic|
  topic.title.gsub!(/-/," ")
  topic.fancy_title.gsub!(/-/," ")
  topic.slug.gsub!(/-/," ")
  puts topic.title
  begin
    topic.save!
  rescue
    puts "oops, something broke."
  end
end

where do we put this code?
and how? later run in terminal :slight_smile:
Thank!

in the rails console:

Yes in console when we run funtion, but where i insert or create new with content:

Topic.where("title LIKE '% %'").find_each do |topic|
  topic.title.gsub!(/-/," ")
  topic.fancy_title.gsub!(/-/," ")
  topic.slug.gsub!(/-/," ")
  puts topic.title
  begin
    topic.save!
  rescue
    puts "oops, something broke."
  end
end

None of this makes sense on many level.

You are searching for all titles with a SPACE and then doing a bunch of substitution on -

@zogstrip suggestion was the correct efficient way of doing this.

Anyway I feel this has run it’s course and any more discussion here is just adding confusion to this topic.

@thaidb you need to run rails c as outlined a few topics up.