Skip to content

Commit

Permalink
Merge pull request #4582 from sanger/fix-metadatable-type-in-polymeta…
Browse files Browse the repository at this point in the history
…data-relationship

Fix metadatable type in poly_metadata relationship on Well
  • Loading branch information
yoldas authored Jan 6, 2025
2 parents b010f3a + 86a6c10 commit 1bd3ea4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/models/well.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions app/resources/api/v2/well_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1bd3ea4

Please sign in to comment.