# Plugin de especialistas por categoria trava ao mesclar usuários

**URL:** https://meta.discourse.org/t/category-experts-plugin-crashes-on-merging-users/399785
**Category:** Bug
**Tags:** category-experts, fixed
**Created:** [Abril 2, 2026, 11:09am UTC](https://meta.discourse.org/t/category-experts-plugin-crashes-on-merging-users/399785 "2026-04-02T11:09:08Z")
**Posts on this page:** 3
**Page:** 1

<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: [Abril 2, 2026, 11:09am UTC](https://meta.discourse.org/t/category-experts-plugin-crashes-on-merging-users/399785/1 "2026-04-02T11:09:08Z")

</div>

Ao mesclar dois usuários via linha de comando, a mesclagem falhou devido a uma falha no plugin de especialistas por categoria. Desativar o plugin antes da mesclagem resolveu o problema.

```plaintext
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Body can't be blank, Body seems unclear, is it a complete sentence? (ActiveRecord::RecordInvalid)
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.4/lib/active_record/validations.rb:87:in `raise_validation_error'
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.4/lib/active_record/validations.rb:54:in `save!'
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.4/lib/active_record/transactions.rb:365:in `block in save!'
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.4/lib/active_record/transactions.rb:417:in `block (2 levels) in with_transaction_returning_status'
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.4/lib/active_record/connection_adapters/abstract/transaction.rb:626:in `block in within_new_transaction'
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activesupport-8.0.4/lib/active_support/concurrency/null_lock.rb:9:in `synchronize'
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.4/lib/active_record/connection_adapters/abstract/transaction.rb:623:in `within_new_transaction'
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:367:in `within_new_transaction'
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:359:in `transaction'
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.4/lib/active_record/transactions.rb:413:in `block in with_transaction_returning_status'
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:416:in `with_connection'
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.4/lib/active_record/connection_handling.rb:312:in `with_connection'
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.4/lib/active_record/transactions.rb:409:in `with_transaction_returning_status'
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.4/lib/active_record/transactions.rb:365:in `save!'
/var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-8.0.4/lib/active_record/suppressor.rb:56:in `save!'
/var/www/discourse/plugins/discourse-category-experts/plugin.rb:351:in `block (2 levels) in activate!'
/var/www/discourse/lib/plugin/instance.rb:637:in `block in on'
/var/www/discourse/lib/discourse_event.rb:12:in `block in trigger'
/var/www/discourse/lib/discourse_event.rb:12:in `trigger'
/var/www/discourse/app/models/post.rb:878:in `set_owner'
/var/www/discourse/app/services/post_owner_changer.rb:30:in `bl

```

[Código](https://github.com/discourse/discourse-category-experts/blob/main/plugin.rb#L351):

```rb
  on(:post_owner_changed) do |post, old_owner, new_owner|
    previously_approved = !post.custom_fields[CategoryExperts::POST_PENDING_EXPERT_APPROVAL]
    post.custom_fields.delete(CategoryExperts::POST_APPROVED_GROUP_NAME)
    post.custom_fields.delete(CategoryExperts::POST_PENDING_EXPERT_APPROVAL)
    post.save!
    CategoryExperts::PostHandler.new(post: post, user: new_owner).process_new_post(
      previously_approved: previously_approved,
    )
  end

```

Como a única modificação ali é em `post.custom_fields`, acredito que `post.save!` seja desnecessário e `post.save_custom_fields` seria suficiente aqui.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [Abril 2, 2026, 3:41pm UTC](https://meta.discourse.org/t/category-experts-plugin-crashes-on-merging-users/399785/3 "2026-04-02T15:41:45Z")

</div>

Obrigado pelo relatório @RGJ 👍

Será corrigido por

> <https://github.com/discourse/discourse-category-experts/pull/230>
>
> …lds changed
> 
> During user merges, \`PostOwnerChanger\` reassigns all post types …including small action posts which have \`raw: nil\` by design. The core \`set\_owner\` method handles this correctly via \`skip\_validations: true\`, but the plugin's \`post\_owner\_changed\` handler and \`PostHandler\` methods were calling \`save!\` after only modifying custom fields. This triggered full AR validations on the post body, raising \`ActiveRecord::RecordInvalid\` for posts with content that doesn't pass current validation rules.
> 
> Replace all \`save!\` calls with \`save\_custom\_fields\` in places where only custom fields are being modified — 8 occurrences across \`plugin.rb\` and \`PostHandler\` (\`mark\_post\_for\_approval\`, \`mark\_post\_as\_approved\`, \`mark\_topic\_as\_question\`, \`correct\_topic\_custom\_fields\_after\_removal\`, \`correct\_topic\_custom\_fields\_after\_addition\`, \`handle\_topic\_category\_change\`).
> 
> https://meta.discourse.org/t/399785

---

<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: [Abril 5, 2026, 3:41pm UTC](https://meta.discourse.org/t/category-experts-plugin-crashes-on-merging-users/399785/5 "2026-04-05T15:41:56Z")

</div>

Este tópico foi automaticamente fechado 3 dias após a última resposta. Novas respostas não são mais permitidas.
