# Using HasCustomFields in a plugin

**URL:** <https://meta.discourse.org/t/using-hascustomfields-in-a-plugin/176469>\
**Category:** Development\
**Created:** [January 18, 2021, 5:50pm UTC](https://meta.discourse.org/t/using-hascustomfields-in-a-plugin/176469 "2021-01-18T17:50:53Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [January 18, 2021, 8:19pm UTC](https://meta.discourse.org/t/using-hascustomfields-in-a-plugin/176469/3 "2021-01-18T20:19:55Z")

</div>

Thanks so much, David!

> [@david](#):
>
> Did you also update the `add_index` call to point to `pfaffmanager_server_id` ?

Yes. That could be my problem. I guess that’s the error message I should have included. And I can make the migration work with either field (though with `pfaffmanager_server_id` as the field name, the index name becomes \> 63 characters, so I have to use a different name for the index.

Let me try again.

### `server_id` as the field name

```ruby
class CreatePfaffmanagerServerCustomField < ActiveRecord::Migration[6.0]
  def change
    create_table :pfaffmanager_server_custom_fields do |t|
      t.integer :server_id, null: false
      t.string :name, limit: 256, null: false
      t.text :value
      t.timestamps null: false
    end
    add_index :pfaffmanager_server_custom_fields, [:server_id, :name]
  end
end

```

Fails on save with:

```plaintext
 Pfaffmanager::ServerCustomField Load (3.8ms) SELECT "pfaffmanager_server_custom_fields".* FROM "pfaffmanager_server_custom_fields" WHERE "pfaffmanager_server_custom_fields"."server_id" = 1
   (1.4ms) ROLLBACK
PG::UndefinedColumn: ERROR: column "pfaffmanager_server_id" of relation "pfaffmanager_server_custom_fields" does not exist
LINE 1: INSERT INTO pfaffmanager_server_custom_fields (pfaffmanager_...
                                                       ^

```

So it’s looking for `pfaffmanager_server_id`. Now, let’s use `pfaffmanager_server_id`.

### `pfaffmanager_server_id` as field name

Here’s the migration:

```ruby
class CreatePfaffmanagerServerCustomField < ActiveRecord::Migration[6.0]
  def change
    create_table :pfaffmanager_server_custom_fields do |t|
      t.integer :pfaffmanager_server_id, null: false
      t.string :name, limit: 256, null: false
      t.text :value
      t.timestamps null: false
    end
    add_index :pfaffmanager_server_custom_fields,
      [:pfaffmanager_server_id, :name],
      name: 'index_pfaffmanager_server_custom_fields_on_server_id_and_name'

  end
end

```

Here’s what happens when I try `s.custom_fields` (the above was able to return `nil` for this but didn’t fail until I tried to **save** a custom field). I `register_custom_field_type` [here](https://github.com/pfaffman/discourse-pfaffmanager/blob/master/app/models/pfaffmanager/server.rb#L23-L28)):

```plaintext
 pry(main)> s.custom_fields
   (1.1ms) SELECT "pfaffmanager_server_custom_fields"."name", "pfaffmanager_server_custom_fields"."value" FROM "pfaffmanager_server_custom_fields" WHERE "pfaffmanager_server_custom_fields"."server_id" = 1 ORDER BY id asc
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column pfaffmanager_server_custom_fields.server_id does not exist
LINE 1: ...e" FROM "pfaffmanager_server_custom_fields" WHERE "pfaffmana...
                                                             ^

from /home/pfaffman/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rack-mini-profiler-2.3.0/lib/patches/db/pg.rb:69:in `exec_params'
Caused by PG::UndefinedColumn: ERROR: column pfaffmanager_server_custom_fields.server_id does not exist
LINE 1: ...e" FROM "pfaffmanager_server_custom_fields" WHERE "pfaffmana...
                                                             ^

```

Now it’s looking for `server_id`.

So it seems like `custom_fields` wants it one way a `save_custom_fields` wants it the other.

It doesn’t care what the index name is, does it?

---

_[View the full topic](https://meta.discourse.org/t/using-hascustomfields-in-a-plugin/176469)._
