Trying to join an association with a view, but the view query disappears. What am I doing wrong?

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?

Relation#join does not use association views, these are only used by Relation#combine.

Is there any way to join a relation while using the relation view?

join and your view are just methods defined on your relation object, just chain them ie your_rel.join(:something).some_other_view.

Yeah but this would only work if you don’t need access to the relations that you join to your current relation. For instance, if I do clients.join(:orders), I don’t have access to any Orders views.

Can you explain what are you trying to do exactly?

I want something like this query:

SELECT * FROM clients
INNER JOIN orders
  ON orders.client_id = clients.id AND orders.status = 0

But I want to start it from the clients relation instead of the orders relation because I might want to filter clients as well. And also I’d like to use the view I defined in the orders relationship for DRYness.

OK that’s just a method:

clients.join(:orders).where(orders[:status].is(0))