# What is the best way to solve following association-scenario?

**URL:** <https://discourse.rom-rb.org/t/what-is-the-best-way-to-solve-following-association-scenario/477>\
**Category:** Uncategorized\
**Created:** [September 2, 2021, 9:51am UTC](https://discourse.rom-rb.org/t/what-is-the-best-way-to-solve-following-association-scenario/477 "2021-09-02T09:51:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![wuarmin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/wuarmin/32/233_2.png) [@wuarmin](https://discourse.rom-rb.org/u/wuarmin)\
**Post date:** [September 2, 2021, 9:51am UTC](https://discourse.rom-rb.org/t/what-is-the-best-way-to-solve-following-association-scenario/477/1 "2021-09-02T09:51:52Z")

</div>

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.

```auto
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
