Hi, due to the legacy nature of my database I have to do a set_table_name() to make sure that the correct MySQL table is picked up by the AR model. For example: model.set_table_name(params[:use_this_table]) One thing that occurred to me is that if I have multiple users accessing my app and they''re looking at different tables then I run the risk that one user''s set_table_name might over-write another''s leading to incorrect data being sent to the user. To mitigate the problem I do the set_table_name as close as possible to the find() or where I write to the database. Is this a valid concern? Are there more techniques to prevent this happening or even detect that it happens? Thanks, Allan --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
<cliveharber-q/Bov5K/xQpeoWH0uzbU5w@public.gmane.org>
2007-Oct-25 13:34 UTC
Re: Ensuring AR#set_table_name is valid with multiple users
Hi You would put the set_table_name definition in the model at the top - just below the class definition line, so you file will look like this: class ModelTable < ActiveRecord::Base set_table_name ''MyTableName'' end As the definition is in the model, then your concern regarding the overwriting of the table names will not be valid - the model takes care of this - each model will have its own set_table_name call. The model models only a single table, and so you only need one set_table_name per model. HTH Clive ---- Allan <Allan.Cochrane-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, > > due to the legacy nature of my database I have to do a > set_table_name() to make sure that the correct MySQL table is picked > up by the AR model. > > For example: > > model.set_table_name(params[:use_this_table]) > > One thing that occurred to me is that if I have multiple users > accessing my app and they''re looking at different tables then I run > the risk that one user''s set_table_name might over-write another''s > leading to incorrect data being sent to the user. > > To mitigate the problem I do the set_table_name as close as possible > to the find() or where I write to the database. > > Is this a valid concern? Are there more techniques to prevent this > happening or even detect that it happens? > > Thanks, > > Allan > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Oct 25, 2:34 pm, <clivehar...-q/Bov5K/xQpeoWH0uzbU5w@public.gmane.org> wrote:> Hi > > You would put the set_table_name definition in the model at the top - just below the class definition line, so you file will look like this: > > class ModelTable < ActiveRecord::Base > set_table_name ''MyTableName'' > > end > > As the definition is in the model, then your concern regarding the overwriting of the table names will not be valid - the model takes care of this - each model will have its own set_table_name call. The model models only a single table, and so you only need one set_table_name per model. >Unfortunately (and I understand this goes against the ''opionionated'' approach of RoR) I would like one model to map onto multiple tables. My rationale: these tables have very minor differences, if any, and there are hundreds of them, I don''t think that one model per table is a realistic option. Each table contains on the order of 100,000 records, some more, many less - the original DB designer sought to minimise the size of the tables, so he effectively ''sharded'' or broke up what could have been a very large table. He also took the opportunity to add or modify columns as each new table was generated. The tables are generated by a process outside the control of my web application, I just have to read from them. I know that AR can map onto the varying tables so a single model that did that seemed to fit nicely, I just had to change the table name and reread the column info. I hope this explains my approach. I have not considered dynamically generating a model for the table on each web call! But I guess I could try some metaprogramming and generate a model per table - but I''d prefer to avoid that if possible! Any other ideas welcomed. Allan Allan --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
<cliveharber-q/Bov5K/xQpeoWH0uzbU5w@public.gmane.org>
2007-Oct-25 14:15 UTC
Re: Ensuring AR#set_table_name is valid with multiple users
Hi Allan You could take a look at Dr Nic''s Magic Models found here: http://magicmodels.rubyforge.org/dr_nic_magic_models/ this might be what you are looking for. Clive ---- Allan <Allan.Cochrane-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Oct 25, 2:34 pm, <clivehar...-q/Bov5K/xQpeoWH0uzbU5w@public.gmane.org> wrote: > > Hi > > > > You would put the set_table_name definition in the model at the top - just below the class definition line, so you file will look like this: > > > > class ModelTable < ActiveRecord::Base > > set_table_name ''MyTableName'' > > > > end > > > > As the definition is in the model, then your concern regarding the overwriting of the table names will not be valid - the model takes care of this - each model will have its own set_table_name call. The model models only a single table, and so you only need one set_table_name per model. > > > > Unfortunately (and I understand this goes against the ''opionionated'' > approach of RoR) I would like one model to map onto multiple tables. > My rationale: these tables have very minor differences, if any, and > there are hundreds of them, I don''t think that one model per table is > a realistic option. Each table contains on the order of 100,000 > records, some more, many less - the original DB designer sought to > minimise the size of the tables, so he effectively ''sharded'' or broke > up what could have been a very large table. He also took the > opportunity to add or modify columns as each new table was generated. > > The tables are generated by a process outside the control of my web > application, I just have to read from them. > > I know that AR can map onto the varying tables so a single model that > did that seemed to fit nicely, I just had to change the table name and > reread the column info. I hope this explains my approach. > > I have not considered dynamically generating a model for the table on > each web call! But I guess I could try some metaprogramming and > generate a model per table - but I''d prefer to avoid that if possible! > > Any other ideas welcomed. > > Allan > > > Allan > > > >--~--~---------~--~----~------------~-------~--~----~ 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
2007-Oct-25 15:53 UTC
Re: Ensuring AR#set_table_name is valid with multiple users
On 25 Oct 2007, at 14:59, Allan wrote:> I know that AR can map onto the varying tables so a single model that > did that seemed to fit nicely, I just had to change the table name and > reread the column info. I hope this explains my approach. >Each mongrel (or fastcgi instance etc....) only handles a single request at a time, so you''ll never have the problem where 1 request changes the value of table_name while a second request assumed it had set it correctly> I have not considered dynamically generating a model for the table on > each web call! But I guess I could try some metaprogramming and > generate a model per table - but I''d prefer to avoid that if possible! >Dr Nic''s magic models does something vaguely similar. If you type Post, it hooks into const_missing, sees that there''s a posts table and creates the Post class for you. Not quire what you need, since your table names aren''t what it expects, but might give you some ideas. assuming all you actual functionality is in ModelTable, you could do this class ModelTable < ActiveRecord::Base self.abstract_class = true ... end and then create subclasses of model table: models_and_tables.each do |model_table| klass = Class.new(ModelTable) Object.const_set(model_table[0], klass) klass.set_table_name model_table[1] end assuming models_and_tables is an array of model name/table name pairs 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''d see this as a HUGE design mistake. That is so thread un-safe. Imagine rails being thread-safe in next 6 months, and you''ll still be stuck with a big fat lock around your request. I''m sure there are other ways to achieve whatever it is you''re trying to, but this is not a right way. Go with the solution which doesn''t change global behavior of your models during request serving. Probably you might end up having 100 models. Which is just fine. params[:use_this_model].classify.find(:all) = GOOOOOD model.set_table_name(params[:use_this_table]) && model.find(:all) = BAD/FUGLY On 10/25/07, Allan <Allan.Cochrane-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, > > due to the legacy nature of my database I have to do a > set_table_name() to make sure that the correct MySQL table is picked > up by the AR model. > > For example: > > model.set_table_name(params[:use_this_table]) > > One thing that occurred to me is that if I have multiple users > accessing my app and they''re looking at different tables then I run > the risk that one user''s set_table_name might over-write another''s > leading to incorrect data being sent to the user. > > To mitigate the problem I do the set_table_name as close as possible > to the find() or where I write to the database. > > Is this a valid concern? Are there more techniques to prevent this > happening or even detect that it happens? > > Thanks, > > Allan > > > > >-- Cheers! - Pratik http://m.onkey.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Stephan Wehner
2007-Oct-25 18:13 UTC
Re: Ensuring AR#set_table_name is valid with multiple users
Allan wrote:> Hi, > > due to the legacy nature of my database I have to do a > set_table_name() to make sure that the correct MySQL table is picked > up by the AR model. > > For example: > > model.set_table_name(params[:use_this_table]) >[...]> To mitigate the problem I do the set_table_name as close as possible > to the find() or where I write to the database.Another option might be to construct AR classes as needed: ar_class = SpecialARConstructor.create_class(params[:use_this_table]) ar_class.find(....) new_row = ar_class.new(....) new_row.save etc. The class definition of the classes created would invoke the desired set_table_name. You would need to use the dynamic features of Ruby when implementing the create_class method in the SpecialARConstructor (module?) Good luck! Stephan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---