title: Changing table name at query run time in a Rails application
body: <p>I have a fat multi-tenant Rails app on Apache + mod_passenger that outputs product prices from a PostgreSQL table as follows:</p>
  
  <pre><code>Table "public.products"
  Column | Type
  id     | bigint
  name   | character varying(100)
  price  | numeric(8,2)
  </code></pre>
  
  <p>Then inside products.rb I have...</p>
  
  <pre><code>class Product &lt; PostgresDatabase
       self.table_name = "products"
  
       # ... yadda yadda
  
  end
  </code></pre>
  
  <p>What I want is to partition the "products" table in a very specific way so that I end up with something like products_TENANT-ID for each tenant (basically views of the main products table but that's another story) and be able to query like this:</p>
  
  <pre><code>Products.for_tenant(TENANT-ID).where(:name =&gt; "My product")......
  </code></pre>
  
  <p>I figure I can just create a method:</p>
  
  <pre><code>class Product &lt; PostgresDatabase
       self.table_name = "products"
  
       # ... yadda yadda
       def for_tenant(tid)
            self.table_name = "products_" + tid.to_s
            self
       end
  end
  </code></pre>
  
  <p>But what kind of impact could this have on the application considering there's lots of traffic (thousands of requests per second)? Is there something I am missing? Should I try a different strategy?</p>
  
  <p>Thank you very much for any feedback/thoughts!</p>