Polymorphic fields with issues

Feature(s) impacted

Some polymorphic fields not displayed correctly.

Observed behavior

Most of the polymorphic fields in our db are now displayed correctly using Polymorphic BelongsTo Typeahead, but some are not.
When trying to edit them that option does note exist.

class Example < ApplicationRecord
  belongs_to :mapped_to, polymorphic: true, optional: true
  belongs_to :something, optional: true
  belongs_to :owner, class_name: 'Something'
end
def Something < Application Record
   has_many :examples, inverse_of: :owner, foreign_key: :owner_id, dependent: :destroy
   # no reference to the actual polymorphic field - maybe that's the issue?
end
{
  "field": "mapped_to",
  "type": "Number",
  "default_value": null,
  "enums": null,
  "integration": null,
  "is_filterable": false,
  "is_read_only": false,
  "is_required": false,
  "is_sortable": true,
  "is_virtual": false,
  "reference": "mapped_to.id",
  "inverse_of": "tpa_mapping",
  "relationship": "BelongsTo",
  "widget": null,
  "validations": [],
  "polymorphic_referenced_models": []
}

Is there a way to force them to be polymorphic in the UI?
Also side question: How does forest determine the polymorphic type options related to a specific field? Is there a way to manually control this?

Expected behavior

Being able to set the field as polymorphic

Context

Recently upgraded forest_liana from 8.0.13 to 9.1.0
Database: postgres (PostgreSQL) 16.2
Rails 7.0.8
Ruby 3.2.3

  • Project name: Hint Health
  • Team name: Hint Health
  • Environment name: Production

Hello @Santiago_Herrero

Welcome on the ForestAdmin community

Can you please add this information so we can better help you ?

  • Project name: …
  • Team name: …
  • Environment name: …

Can you please also share with us you models declarations for both part of the relation ?

Thanks @nbouliol - Updated my original message with the requested info

1 Like

Hi @Santiago_Herrero,

Exactly, you need to add the reference to the polymorphic relationship.
Unfortunately, there is currently no way of controlling this manually.

1 Like

As previously discussed by @Santiago_Herrero , we have implemented a polymorphic field following the example provided below:

class Example < ApplicationRecord
  belongs_to :mapped_to, polymorphic: true, optional: true
end

class Something < Application Record
  has_many :examples, as: :mapped_to
end

class SomethingDifferent < Application Record
  has_many :examples, as: :mapped_to
end

While attempting to create a record using Forest Admin, we encounter the type of the field can be selected without any difficulty, and the associated data can be specified accordingly. However, upon attempting to save the record, the operation fails.

Upon investigation, it was observed that while the mapped_to_id is correctly set, the mapped_to_type field remains empty. Curiously, saving records works seamlessly when we select type Something, it fails when we select type SomethingDifferent, despite both being defined identically.

We are uncertain if there are additional configurations required or if any crucial details may have been overlooked. Any guidance or insight on this matter would be greatly appreciated.

Hi @Romina_Berrutti, and welcome in our community !

I’ve just released a new version of the forest_liana(V9.3.4) that fix this issue.

1 Like

Great, thank you @nicolasa !!

1 Like