Here are my relations:
class Clients < ROM::Relation[:sql]
schema(infer: true) do
associations do
has_many :orders
has_one :order, as: :cart, view: :carts_for_clients, override: true
end
end
end
class Orders < ROM::Relation[:sql]
STATUSES = {
cart: 0,
started: 1
}.freeze
schema(infer: true) do
associations do
belongs_to :client
end
end
def carts
where(status: STATUSES[:cart])
end
def carts_for_clients(_assoc, clients)
carts.where(client_id: clients.map { |c| c[:id] })
end
end
When I combine them, everything seems hunky-dory. The client gets their cart fetched.
But when I try and join them, I see this:
pry(main)> clients.join(:cart)
=> #<Clients name=ROM::Relation::Name(clients) dataset=#<Sequel::Postgres::Dataset: "SELECT \"clients\".\"id\", \"clients\".\"login\", \"clients\".\"display_name\" FROM \"clients\" INNER JOIN \"orders\" ON (\"clients\".\"id\" = \"orders\".\"client_id\") ORDER BY \"clients\".\"id\"">>
As you can see, there’s no reference of filtering orders by their status.
And also, if I do clients.join(:cart).to_a
, I get clients that currently have no carts (but have other orders).
Is there something I’m doing wrong?