FrikShun
2008-Apr-16 23:05 UTC
How to dynamically set table_name of model object without concurrency issues?
Hello all, I''m trying to figure out a good way to dynamically set the table_name of an ActiveRecord model object. I''ve got a legacy db with multiple tables which hold the same type of data. The tables are laid out as DATA1000 through DATA9999. My app needs to allow the user to select a number 1000 through 9999 and then use that selection in order to determine the appropriate table to query. I have a single ActiveRecord object called DataX which I''d like to use to access all of the above tables. What I''ve been doing is to call DataX.set_table_name for every search depending on the users selection to get the data from the right table. This is not going to work in a multi-user env as set_table_name is a class method and I''d be resetting the table name for all users, all sessions. Does anyone have any other suggestions for dynamically assigning a new table name to an instance of an ActiveRecord that is scoped at the user session level? Thanks, Allen --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Apr-17 07:19 UTC
Re: How to dynamically set table_name of model object without concurrency issues?
On Apr 17, 12:05 am, FrikShun <a...-UM300TINZ1VWk0Htik3J/w@public.gmane.org> wrote:> Hello all, > > I''m trying to figure out a good way to dynamically set the table_name > of an ActiveRecord model object. > > I''ve got a legacy db with multiple tables which hold the same type of > data. The tables are laid out as DATA1000 through DATA9999. > > My app needs to allow the user to select a number 1000 through 9999 > and then use that selection in order to determine the appropriate > table to query. > > I have a single ActiveRecord object called DataX which I''d like to use > to access all of the above tables.Make DataX abstract and dynamically create subclasses as needed ? It''s not pretty but given that (right now) a single instance (mongrel process, fastcgi listener etc..) of rails only processes one request at a time you could probably get away with calling set_table_name on DataX Fred --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---