ActiveRecord dynamic table naming with has_many possible?

A legacy database I’m currently working with is setup such that each
client (in a clients table) has a field containing the name of the
associated users table (which is distinct for each client).

What I’d like to be able to do is take advantage of ActiveRecord’s
has_many (in this case :users) association, but I’m drawing a blank.

My original thinking was to either use "has_many :users, Both
has_many and set_table_name are class methods

Is this at all possible? My original thinking was to “sneak-one-in”
and do something like this:

class User < ActiveRecord::Base
belongs_to :client
set_table_name ‘#{client.users_table}’

…but set_table_name is a class method, plus it’s probably a chicken-
egg thing as well.

Another way which struck me a possible was to generate a User model
for each client dynamically, but unfortunately this is where my ruby
skills go on holiday:

class Client
def after_find
dynamic_model = "
class User_#{user_table} < AbstractUser
set_table_name ‘#{user_table}’

   has_many users, :class_name => User_#{user_table}


…again I’m caught out by the has_many association class method.

Does anyone out there have an idea as to whether what I’m trying to
do is at all possible? Admittedly my Ruby (and Rails for that matter)
is incredibly rusty, buy any pointers would be great.



John M. [email protected]

“Life creates itself in delirium and is undone in ennui.”

  • Emile M. Cioran