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

class Orders < ROM::Relation[:sql]
    cart: 0,
    started: 1

  schema(infer: true) do
    associations do
      belongs_to :client

  def carts
    where(status: STATUSES[:cart])

  def carts_for_clients(_assoc, clients)
    carts.where(client_id: { |c| c[:id] })

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
  ON orders.client_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: