I have a model in a plugin. The model is Pfaffmananger::Server
, the table is pfaffmanager_servers
. For Reasons, I want to have custom fields for this model (I expect there to be ways that I want to extend the server data that are likely to be used only for a few instances and I don’t want a zillion mostly-unused fields in the model).
Here’s the migration that creates the table:
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
If I use pfaffmanager_server_id
instead of server_id
the migration fails because it can’t find server_id
, but when I use pfaffmanager_server_id
for the field name, when I go to save a server after I create a custom field, I get:
Pfaffmanager::ServerCustomField Load (0.4ms) SELECT "pfaffmanager_server_custom_fields".* FROM "pfaffmanager_server_custom_fields" WHERE "pfaffmanager_server_custom_fields"."server_id" = 1
(0.2ms) 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 if the migration has field pfaffmanager_server_id
the migration fails, and if the field name is server_id
the save fails.
I’m looking at save_custom_fields
in concers/has_custom_fields.rb
, but can’t quite figure out if there’s some way to override that.
Is there something that I can do besides refactor the whole thing to use server rather than pfaffmanager_server?