From 86a6c10d934ed3e0cca91d33cfe8993c3d3b6128 Mon Sep 17 00:00:00 2001 From: yoldas Date: Mon, 6 Jan 2025 10:01:31 +0000 Subject: [PATCH] Override the autogenerated poly_metadata method to pick the type correctly --- app/models/well.rb | 13 +++++++++++++ app/resources/api/v2/well_resource.rb | 2 ++ 2 files changed, 15 insertions(+) diff --git a/app/models/well.rb b/app/models/well.rb index d40ad694d6..398fb8bde5 100644 --- a/app/models/well.rb +++ b/app/models/well.rb @@ -48,6 +48,19 @@ def attach(wells) has_many :target_well_links, -> { stock }, class_name: 'Well::Link', foreign_key: :source_well_id has_many :target_wells, through: :target_well_links, source: :target_well + # Can have many key value pairs of metadata + has_many :poly_metadata, as: :metadatable, dependent: :destroy + + # Returns a collection of PolyMetadatum records associated with the Well. + # This method overrides the autogenerated poly_metadata method to pick the + # correct metadatable_type instead of the parent type. Without this override, + # the generated SQL uses the wrong class name, e.g. Receptacle instead of Well. + # + # @return [ActiveRecord::Relation] a collection of PolyMetadatum records + def poly_metadata + PolyMetadatum.where(metadatable_id: id, metadatable_type: self.class.name) + end + belongs_to :plate, foreign_key: :labware_id has_one :well_attribute, inverse_of: :well diff --git a/app/resources/api/v2/well_resource.rb b/app/resources/api/v2/well_resource.rb index 21574734df..273d24aec1 100644 --- a/app/resources/api/v2/well_resource.rb +++ b/app/resources/api/v2/well_resource.rb @@ -21,6 +21,8 @@ class WellResource < BaseResource # Attributes attribute :position, readonly: true + has_many :poly_metadata, as: :metadatable, class_name: 'PolyMetadatum' + def position { 'name' => _model.map_description } end