Jim Stewart
2007-Jun-06 19:50 UTC
ActiveRecord: Dynamically-named tables sharing a common model
I''ve got a legacy database that I''m trying to model in ActiveRecord. The database consists of many pairs of tables (e.g. Foo_Sources and Foo_Procedures, Bar_Sources and Bar_Procedures). There will be multiple pairs of tables, serving different purposes but sharing a common schema (Foo_Sources and Bar_Sources will be identical except for the table name). I''d like to model these in ActiveRecord, but I''m not sure how to approach the problem. The tables I access will be determined at runtime, and the database itself is dynamic (Baz_Sources will appear in the future, and I don''t want to have to change the code to support it). Anyone have any ideas? I''m a relative newcomer to Ruby/Rails, but I get the impression something like this could be handled since Ruby is such a dynamic language. Maybe some kind of ActiveRecord class factory? Any suggestions would be appreciated! Thanks in advance. Jim --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
tmac
2007-Jun-06 20:10 UTC
Re: ActiveRecord: Dynamically-named tables sharing a common model
Sounds like a case for STI (single table inheritance) to me. For examlple: *create a table named sources and it''s associated model with all your attributes plus the "type" attribute *create your models e.g... FooSources, BarSources that inherit from Sources *create a little script, or just do it manually from your db client, to move all the data to the new table something like that anyway;-) Now anytime you want to create another table you don''t have to, you just create a new model that inherits from Sources. Hope that made some sense? A bit more on STI in rails here: http://www.juixe.com/techknow/index.php/2006/06/03/rails-single-table-inheritance/ Good Luck! Tim On Jun 6, 12:50 pm, Jim Stewart <james.a.stew...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve got a legacy database that I''m trying to model in ActiveRecord. > The database consists of many pairs of tables (e.g. Foo_Sources and > Foo_Procedures, Bar_Sources and Bar_Procedures). There will be > multiple pairs of tables, serving different purposes but sharing a > common schema (Foo_Sources and Bar_Sources will be identical except > for the table name). > > I''d like to model these in ActiveRecord, but I''m not sure how to > approach the problem. The tables I access will be determined at > runtime, and the database itself is dynamic (Baz_Sources will appear > in the future, and I don''t want to have to change the code to support > it). > > Anyone have any ideas? I''m a relative newcomer to Ruby/Rails, but I > get the impression something like this could be handled since Ruby is > such a dynamic language. Maybe some kind of ActiveRecord class > factory? > > Any suggestions would be appreciated! Thanks in advance. > > Jim--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
MichaelLatta
2007-Jun-06 20:12 UTC
Re: ActiveRecord: Dynamically-named tables sharing a common model
There are many approaches to this. First let me point out that unlike almost all other languages, in Ruby you can create classes at run-time in the language. So you can execute "foo < ActiveRecord::Base" whenever you like for any table you know to exist. You can use a static table to get a connection to the database and execute any SQL you like to obtain the list of tables you care about. Since ActiveRecord looks at the database table for attribute data the fact that they share schema is coincidental but does not really affect the issue. Since ruby uses duck typing you do not need to tell the system they share a schema, you just happen to use the same methods to access their data. In your Rails application you just need to add the proper bootstrap code to look for all the dynamic tables and build ActiveRecord subclasses for each table. Michael On Jun 6, 12:50 pm, Jim Stewart <james.a.stew...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve got a legacy database that I''m trying to model in ActiveRecord. > The database consists of many pairs of tables (e.g. Foo_Sources and > Foo_Procedures, Bar_Sources and Bar_Procedures). There will be > multiple pairs of tables, serving different purposes but sharing a > common schema (Foo_Sources and Bar_Sources will be identical except > for the table name). > > I''d like to model these in ActiveRecord, but I''m not sure how to > approach the problem. The tables I access will be determined at > runtime, and the database itself is dynamic (Baz_Sources will appear > in the future, and I don''t want to have to change the code to support > it). > > Anyone have any ideas? I''m a relative newcomer to Ruby/Rails, but I > get the impression something like this could be handled since Ruby is > such a dynamic language. Maybe some kind of ActiveRecord class > factory? > > Any suggestions would be appreciated! Thanks in advance. > > Jim--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jim Stewart
2007-Jun-08 00:03 UTC
Re: ActiveRecord: Dynamically-named tables sharing a common model
That''s roughly the idea I had in mind when I mentioned an ActiveRecord class factory. I think it''ll work well. The current temporary solution is to just call :set_table_name on the model class as needed. It works, but there are plenty of limitations to that approach. Thanks for the tip. Jim On Jun 6, 1:12 pm, MichaelLatta <lat...-ee4meeAH724@public.gmane.org> wrote:> There are many approaches to this. First let me point out that unlike > almost all other languages, in Ruby you can create classes at run-time > in the language. > > So you can execute "foo < ActiveRecord::Base" whenever you like for > any table you know to exist. You can use a static table to get a > connection to the database and execute any SQL you like to obtain the > list of tables you care about. Since ActiveRecord looks at the > database table for attribute data the fact that they share schema is > coincidental but does not really affect the issue. Since ruby uses > duck typing you do not need to tell the system they share a schema, > you just happen to use the same methods to access their data. > > In your Rails application you just need to add the proper bootstrap > code to look for all the dynamic tables and build ActiveRecord > subclasses for each table. > > Michael > > On Jun 6, 12:50 pm, Jim Stewart <james.a.stew...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''ve got a legacy database that I''m trying to model in ActiveRecord. > > The database consists of many pairs of tables (e.g. Foo_Sources and > > Foo_Procedures, Bar_Sources and Bar_Procedures). There will be > > multiple pairs of tables, serving different purposes but sharing a > > common schema (Foo_Sources and Bar_Sources will be identical except > > for the table name). > > > I''d like to model these in ActiveRecord, but I''m not sure how to > > approach the problem. The tables I access will be determined at > > runtime, and the database itself is dynamic (Baz_Sources will appear > > in the future, and I don''t want to have to change the code to support > > it). > > > Anyone have any ideas? I''m a relative newcomer to Ruby/Rails, but I > > get the impression something like this could be handled since Ruby is > > such a dynamic language. Maybe some kind of ActiveRecord class > > factory? > > > Any suggestions would be appreciated! Thanks in advance. > > > Jim--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---