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["-"," "]
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
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
#<PG::Result:0x00007fc60eab0ca8 status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=0>
no change has been made to titles
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?
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
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.
@huynhthai824 you need to run rails c
as outlined a few topics up.