The problem I have faced is the following: with_tenant method works correctly only for cases where ActiveRecord relation queries db inside the with_tenant block. Otherwise, it is simply ignored.
In other words it works fine here (let's say there are 4 items and only 3 are with the tenant):
Multitenant.with_tenant @tenant do
@items = Item.where("name IS NOT NULL").to_a
end
puts @items.count # 3; all correct
But fails to work here:
Multitenant.with_tenant @tenant do
@items = Item.where("name IS NOT NULL")
end
puts @items.count # 4; incorrect!
Spec that indicates the failure can be found here 81fcb09