# שינוי שם תגיות בקונסולת rails

**URL:** https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375
**Category:** Support
**Tags:** rails-console, tags
**Created:** [6 ביוני,‏ 2023,‏ 11:07am UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375 "2023-06-06T11:07:43Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Campano](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/campano/32/277419_2.png) [@Campano](https://meta.discourse.org/u/Campano)
#### Post date: [6 ביוני,‏ 2023,‏ 11:07am UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375/1 "2023-06-06T11:07:43Z")

</div>

Continuing the discussion from [Can’t fix tag names with dots/periods](https://meta.discourse.org/t/can-t-fix-tag-names-with-dots-periods/141706/2):

> [@Can’t fix tag names with dots/periods](https://meta.discourse.org/t/can-t-fix-tag-names-with-dots-periods/141706/2):
>
> If that’s the case then you’ll need to rename them in the rails console.

Hello,

Can anyone provide a tested guide on how to rename tags in the rails console? I have no experience using the rails console and don’t want to mess up the database…

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [6 ביוני,‏ 2023,‏ 2:11pm UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375/2 "2023-06-06T14:11:52Z")

</div>

SSH into your server

```plaintext
cd /var/discourse
./launcher enter app
rails c

t = Tag.find_by_name('oldname')
t.name = 'newname'
t.save

```

---

<div class="post-metadata">

### Author: ![AquaL1te](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aqual1te/32/201966_2.png) [@AquaL1te](https://meta.discourse.org/u/AquaL1te)
#### Post date: [9 ביוני,‏ 2023,‏ 6:18am UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375/3 "2023-06-09T06:18:08Z")

</div>

Is there also a way to change a specific tag in all topics? I renamed my domain once, there was a method to rewrite all references to the old domain do the new one. Can this also be done with tags? I want to rename a tag, but not confuse users with non-existing tags.

Edit, solved it with tag synonyms 🎉 Although it’s far from ideal, since it will continue the use of old tags and it’s shown in my tags overview and auto-complete. I would really want to replace it fully and obsolete the old one.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [9 ביוני,‏ 2023,‏ 7:47am UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375/4 "2023-06-09T07:47:27Z")

</div>

Honest question - how is renaming the tag not sufficient for this?

---

<div class="post-metadata">

### Author: ![AquaL1te](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aqual1te/32/201966_2.png) [@AquaL1te](https://meta.discourse.org/u/AquaL1te)
#### Post date: [9 ביוני,‏ 2023,‏ 8:35am UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375/5 "2023-06-09T08:35:27Z")

</div>

Well for example when I use the tag #chat here and may others as well in different topics, and then you rename it to `#chats`. Then the `#chat` tag will be pointing to an non-existing tag because it’s not renamed. So the renaming works fine for topic titles, but not for within the topic comments when people used them already. Or am I missing something?

---

<div class="post-metadata">

### Author: ![Campano](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/campano/32/277419_2.png) [@Campano](https://meta.discourse.org/u/Campano)
#### Post date: [9 ביוני,‏ 2023,‏ 9:11am UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375/7 "2023-06-09T09:11:07Z")

</div>

Extra thanks @RGJ , worked like a breeze 🙏 🙏 🙏

---

<div class="post-metadata">

### Author: ![AquaL1te](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aqual1te/32/201966_2.png) [@AquaL1te](https://meta.discourse.org/u/AquaL1te)
#### Post date: [14 ביוני,‏ 2023,‏ 4:55pm UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375/8 "2023-06-14T16:55:36Z")

</div>

I guess there is no way to rename existing tags used in topics by users? So not the tags in topic titles, but really in the comments.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [14 ביוני,‏ 2023,‏ 5:07pm UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375/9 "2023-06-14T17:07:45Z")

</div>

Use at your own risk!

Do this _after_ you have renamed `#foo` to `#bar`

```plaintext
Post.where("raw like '%#foo%'").each do |p|
  p.raw = p.raw.gsub(/#foo\b/, '#bar')
  p.save!
end

```

---

<div class="post-metadata">

### Author: ![AquaL1te](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aqual1te/32/201966_2.png) [@AquaL1te](https://meta.discourse.org/u/AquaL1te)
#### Post date: [14 ביוני,‏ 2023,‏ 5:19pm UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375/10 "2023-06-14T17:19:42Z")

</div>

Looks cool! Thanks for the heads-up. Backups will mitigate the risk 🤓.

For my understanding, `"raw like '%#foo%'"`, is that strict matching? In the sense of, what if you have tag `#access` which needs to be renamed to `#access_granted`, would `"raw like '%#access%'"` match both `#access` and `#access_granted`? Because in that case existing comments that already use `#access_granted` could maybe be renamed to `#accessaccess_granted`.

To compare it to the `grep` command, the switch `--word-regexp` helps out and makes sure you match whole words only.

From the man page:

> Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.

Does this command work similar? Of course I can find out by doing the backup and run the command. But just to understand the risks, if you know this of course. Otherwise, YOLO.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [14 ביוני,‏ 2023,‏ 5:50pm UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375/11 "2023-06-14T17:50:54Z")

</div>

The `"raw like '%#foo%'"` is indeed not strict matching, but it does not need to be. Because a LIKE % query is very expensive performance wise, I chose to not make it even worse by requiring a non-word-character after it. The `where` query is just there to pre-select - you could actually do `Post.all.each` and it would work as well (albeit much slower).

The _actual_ replacing is being done by the `gsub` and the `\b` ensures that there is a word boundary after  
the tag, i.e. the tag ends there.

So any posts containing `#access_granted` would match, and the inner code would execute, but the `gsub` would not do anything in that case.

```plaintext
irb(main):010:0> "Tag #access is here".gsub(/#access\b/, "#access_granted")
=> "Tag #access_granted is here"
irb(main):011:0> "Tag #access_granted is here".gsub(/#access\b/, "#access_granted")
=> "Tag #access_granted is here"

```

---

<div class="post-metadata">

### Author: ![AquaL1te](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aqual1te/32/201966_2.png) [@AquaL1te](https://meta.discourse.org/u/AquaL1te)
#### Post date: [14 ביוני,‏ 2023,‏ 7:13pm UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375/12 "2023-06-14T19:13:22Z")

</div>

I want to run this now:

```plaintext
Post.where("raw like '%#access%'").each do |p|
  p.raw = p.raw.gsub(/#access\b/, '#strategic_access')
  p.save!
end

Post.where("raw like '%#feedback%'").each do |p|
  p.raw = p.raw.gsub(/#feedback\b/, '#digital_feedback')
  p.save!
end

```

But I get an error `bash: syntax error near unexpected token `“raw like ‘%#access%’”'`and`bash: syntax error near unexpected token `"raw like '%#feedback%'"'`, before I start guessing, does the `#` need to be escaped?

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [14 ביוני,‏ 2023,‏ 8:01pm UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375/13 "2023-06-14T20:01:05Z")

</div>

> [@RGJ](#):
>
> SSH into your server
> 
> ```plaintext
> cd /var/discourse
> ./launcher enter app
> rails c
> 
> t = Tag.find_by_name('oldname')
> t.name = 'newname'
> t.save
> 
> ```

☝ it was supposed to come _after_ this, the code should be executed within `rails c`

---

<div class="post-metadata">

### Author: ![AquaL1te](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aqual1te/32/201966_2.png) [@AquaL1te](https://meta.discourse.org/u/AquaL1te)
#### Post date: [17 ביוני,‏ 2023,‏ 8:55am UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375/14 "2023-06-17T08:55:30Z")

</div>

Oh yes, I didn’t even notice the bash error. I should get more sleep… Thanks!!

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [17 ביולי,‏ 2023,‏ 8:55am UTC](https://meta.discourse.org/t/renaming-tags-in-the-rails-console/267375/15 "2023-07-17T08:55:31Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
