John Meredith
2007-Sep-25 14:27 UTC
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}'' end ...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}'' end has_many users, :class_name => User_#{user_table} " eval(dynamic_model) end end ...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. TIA, John -- John Meredith <statebase-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> "Life creates itself in delirium and is undone in ennui." - Emile M. Cioran --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---