Suppose a column client_id is ubiquitous through our database, as well as for confirmed session or request, we are 'in the context' of the client the entire time.

It is possible to method to simulate getting each client's data saved inside a separate database, and keep them within the same table for simpler database-management ? Things I want is comparable to a default scope, consider it'll change for each request, it cant' be fixed at load-time.

  # invoices table data
  # -------------------
  # id       client_id     amount
  # 1        1             100.00
  # 2        2             100.00
  with_client( Client.find(1) ) do
     Invoices.all      # finds only invoice 1
     Invoices.find(2)  # finds nothing or raises

How do i do that with ActiveRecord, or at what points could I surgically alter AR to affect this behavior ?

Extra points: Id prefer to avoid the upgrading of the client_id column - it ought to be fixed at create-time

class Client < ActiveRecord::Base
  has_one :invoice, :dependent => :destroy

class Invoice < ActiveRecord::Base
  belongs_to :client

In controller

   @client= Client.find(1)
   @client.invoice #finds  id  =1     client_id  =1   amount=100.0

There's an exhibition here that talks about multi-tenanted programs. It's some interesting suggestions to utilise database schemas, particularly for postgres.