ManyToMany when through relationship must be resolved before the has_many


#1

Greetings!

I am working on ROM adapter for an old SOAP API and I am current stuck on a problem with ManyToMany associations. As a bit of background, here is what my relations look like:

ROM::Configuration.new(:adwords, client: client) do |config|
  config.relation(:campaigns) do
    schema(infer: true) do
      attribute :id, Gladwords::Types::ID

      primary_key :id

      associations do
        has_many :ad_groups, combine_key: :campaign_id
        has_many :ad_group_ads, through: :ad_groups
      end
    end
  end

  config.relation(:ad_groups) do
    schema(infer: true) do
      attribute :id, Gladwords::Types::ID

      primary_key :id

      associations do
        belongs_to :campaign, combine_key: :id
        has_many :ad_group_ads, combine_key: :ad_group_id
      end
    end
  end

  config.relation(:ad_group_ads) do
    schema(infer: true) do
      associations do
        belongs_to :ad_group
      end
    end
  end
end

In order to make this configuration work, I will have to resolve the ad_groups relationship before I resolve the ad_group_ads relationship. This way, I can get all the ad_group_ads.where(ad_group_id: [1,2,3]). However, within my ManyToMany association class, I only receive .call with a target of ROM::Relation::AdGroupAds and a source of ROM::Relation::Campaigns.

In my preload method, I could resolve the data fine if I could ensure that ad_groups were combined on campaigns. i.e. campaigns.combine(:ad_groups). This way, I would have a list of ad_groups that I could then filter ad_group_ads by.

Is the a straightforward way of modifying the source relation within a ManyToMany association so I could make this work?

Hopefully that all makes sense and doesn’t scare everyone away! If what I said sounds like a mad-man rambling, please ask me to clarify. :grinning:

Thanks in advance,
Ian