#57904·rails

where(association: nil) on a composite foreign key emits IS NULL on shared (tenant) columns, breaking multi-tenant scopes

Author: ollymCreated Jun 28, 2026Updated Sep 27, 2026

Steps to reproduce

When a belongs_to uses a composite foreign key whose columns are shared with the model's scoping (a classic multi-tenant shop_id that is part of query_constraints and of more than one composite FK on the same model), where(association: nil) expands to IS NULL on every column of the composite key — including the shared shop_id.

That extra shop_id IS NULL predicate contradicts any tenant scope already on the relation, silently turning it into an always-empty result.

The model below has shop_id plus two composite FKs (first_item, second_item) that both reuse shop_id. Querying => nil on just one of them forces shop_id IS NULL:

ruby
# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"
  gem "rails", "8.1.3" # also reproduces on `branch: "main"` (see note below)
  gem "sqlite3"
end

require "active_record"
require "minitest/autorun"
require "logger"

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new($stdout)

ActiveRecord::Schema.define do
  create_table :shops, force: true
  create_table :items, force: true do |t|
    t.integer :shop_id
  end
  create_table :orders, force: true do |t|
    t.integer :shop_id
    t.integer :first_item_id
    t.integer :second_item_id
  end
end

class Shop < ActiveRecord::Base; end

class Item < ActiveRecord::Base
  query_constraints :shop_id, :id
end

class Order < ActiveRecord::Base
  query_constraints :shop_id, :id

  belongs_to :shop
  # Two composite belongs_to on the same model; both share the tenant column shop_id.
  belongs_to :first_item,  class_name: "Item", foreign_key: [:shop_id, :first_item_id], optional: true
  belongs_to :second_item, class_name: "Item", foreign_key: [:shop_id, :second_item_id], optional: true
end

class CompositeNilWhereTest < Minitest::Test
  def test_where_nil_on_one_composite_assoc_nulls_the_shared_tenant_column
    shop = Shop.create!
    order = Order.create!(shop_id: shop.id, first_item_id: nil, second_item_id: nil)

    # "orders in this shop that have no first_item" — should return the order above.
    relation = Order.where(shop_id: shop.id).where(first_item: nil)

    puts "\nGENERATED SQL:\n#{relation.to_sql}\n\n"

    assert_equal [order.id], relation.pluck(:id),
      "expected the order (shop_id set, first_item_id nil) to be returned"
  end
end

Expected behavior

Order.where(shop_id: shop.id).where(first_item: nil) should return orders in that shop whose first_item_id is NULL, i.e.:

sql
SELECT "orders".* FROM "orders" WHERE "orders"."shop_id" = 1 AND "orders"."first_item_id" IS NULL

Actual behavior

The composite-key expansion also nulls the shared shop_id, contradicting the tenant scope, so the relation is always empty:

sql
SELECT "orders".* FROM "orders" WHERE "orders"."shop_id" = 1 AND "orders"."shop_id" IS NULL AND "orders"."first_item_id" IS NULL

The test fails ([] returned instead of [order.id]).

The relevant code is ActiveRecord::PredicateBuilder::AssociationQueryValue#convert_to_id (its nil handling was added in #53706):

ruby
def convert_to_id(value)
  if primary_key.is_a?(Array)
    primary_key.map do |attribute|
      next nil if value.nil?
      # ...
    end
  # ...

For a nil association value it emits nil for every column of the composite key. When one of those columns is shared (a tenant column reused across query_constraints and several composite FKs), nulling it for a single => nil association invalidates the rest of the query. The two composite FKs in the example make the sharing explicit: where(first_item: nil) nulls the shop_id that also belongs to second_item and to the model's own scope.

It seems like where(association: nil) should only constrain the column(s) that uniquely identify that association's target (here first_item_id), not columns it merely shares with the owner's scope / other associations.

System configuration

Rails version: 8.1.3 (verified). Also present on main: AssociationQueryValue#convert_to_id is unchanged since #53706 — replace the gemfile line with gem "rails", github: "rails/rails", branch: "main" to reproduce on edge.

Ruby version: 4.0.5