Hello,
I have following tables:
main-table
records (id, source_name, source_id, …), i.e. [{15, “source_one”, 89}, {16, “source_two”, 89]
source-tables
source_one_records (id, …)
source_two_records (id, …)
Every record has a connection to one source-table-record .
Following snippet show my association setting of Relation::Records , to be able to combine i.e. a source_one_record or source_two_record, if available. I tried to solve it with custom-views.
Am I on the right direction, or is there a simpler solution? So far i haven’t had any success. The attribute source_one is nil in Records::Entitiy, although the view’s sql is executed and returns a valid relation.
module Orm
module Relations
class Records < ROM::Relation[:sql]
schema(:records, infer: true) do
associations do
has_one :source_one_record, view: :for_records, override: true
has_one :source_two_record, view: :for_records, override: true
end
end
end
end
end
# frozen_string_literal: true
module Orm
module Relations
class SourceOneRecords < ROM::Relation[:sql]
schema(:source_one_records, infer: true)
def for_records(assoc, records)
join(:records, source_id: :id, source_name: "source_one")
.where { assoc.source[:id] =~ records.pluck(:id) }
end
end
end
end
Thanks and best regards
Armin