How to replace a string/character in topic title?

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["-"," "]

1 Like

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.

1 Like

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, '-', ' ')")
5 Likes

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.

1 Like

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

2 Likes
#<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
1 Like

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.

3 Likes

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
2 Likes

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

in the rails console:

2 Likes

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.

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