From 0d96decc40b1a5dd9e8188f8d0ede4599913938b Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 14 Nov 2024 11:14:11 +0200 Subject: [PATCH] quick fix --- lib/avo/fields/belongs_to_field.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/avo/fields/belongs_to_field.rb b/lib/avo/fields/belongs_to_field.rb index b70e5b626..6a4a4a96b 100644 --- a/lib/avo/fields/belongs_to_field.rb +++ b/lib/avo/fields/belongs_to_field.rb @@ -161,7 +161,7 @@ def primary_key @primary_key ||= reflection.association_primary_key # Quick fix for "Polymorphic associations do not support computing the class." rescue - id + nil end def foreign_key @@ -218,12 +218,22 @@ def fill_field(record, key, value, params) if valid_model_class.blank? || id_from_param.blank? record.send(:"#{polymorphic_as}_id=", nil) else - record_id = target_resource(record:, polymorphic_model_class: value.safe_constantize).find_record(id_from_param).send(primary_key) + record = target_resource(record:, polymorphic_model_class: value.safe_constantize).find_record(id_from_param) + record_id = if primary_key.present? + record.send(primary_key) + else + record.id + end record.send(:"#{polymorphic_as}_id=", record_id) end else - record_id = value.blank? ? value : target_resource(record:).find_record(value).send(primary_key) + record = value.blank? ? value : target_resource(record:).find_record(value) + record_id = if primary_key.present? + record.send(primary_key) + else + record.id + end record.send(:"#{key}=", record_id) end