How to replace a string/character in topic title?


(Pad Pors) #1

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


Replace a string in all posts
Automatic encoding of parsed URL params
#2

As mentioned before, use regular expressions :slightly_smiling_face:


(Pad Pors) #3

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


#4

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.


(Régis Hanol) #5

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

(Pad Pors) #6

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

any other suggestion would be appreciated.


(Régis Hanol) #7

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


(Pad Pors) #8

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.


(Régis Hanol) #9

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


(Pad Pors) #10
#<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


(Jay Pfaffman) #11

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

Automatic encoding of parsed URL params
(Pad Pors) #12

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

(Jay Pfaffman) #13

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.


(Pad Pors) #14

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.


(Jay Pfaffman) #15

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


(Pad Pors) #16

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