rom-rb / rom-http

Abstract HTTP adapter for ROM
https://rom-rb.org
MIT License
73 stars 18 forks source link

Combining data with sql relations #36

Open firstsano opened 5 years ago

firstsano commented 5 years ago

Defining an associaton gives me a finalize_associations!' exception, so I guess it's not implemented. I need to combine a http relation with a sql relation. Am I not mistaking that the only option I have is to use combine_with?

solnic commented 5 years ago

Yes you could try that. We'll add support for associations eventually.

firstsano commented 5 years ago

Thanks for response! Okay. Now here's the tricky part. Suppose I have an http relation:

class UsersRelation < ::ROM::Relation[:http]
    gateway :my_awesome_gateway

    schema(:users) do
      attribute :id, Types::Int
      attribute :login, Types::Strict::String
      attribute :car_id, Types::Int
      primary_key :id
    end

    def by_id(id)
      with_path(id.to_s)
    end
  end

And an sql relation:

class CarsRelation < ::ROM::Relation[:sql]
  gateway :my_precious_gateway

  schema(:cars, infer: true)

  dataset do
    where(is_deleted: 0).order(:id)
  end

  def by_id(id)
    where(id: id)
  end

  def for_users(_assoc, users)
    where(id: users.map { |u| u[:car_id] })
  end
end

So when I use users.by_id(id).combine_with(cars.for_users).one it throws

ROM::MapperMisconfiguredError ([[:id], [:name], [:create_date], [:change_date], [:description]] attribute: block is required for :from with union value.):

where above-mentioned attributes are the attributes of an entity, representing a car in a database. Where should I look for the problem? I've read a lot of comments in gitter with the same problem, and I believe that the code should work.