Matt.C.Wagner@wellsfargo.com
2006-Jan-03 17:08 UTC
[Rails] Changing tables within rails app
Hi, I want to know if my idea is possible within the rails framework. I want to write a rails app that uses the same code to manage two classes of widgets; "wired widgets" and "wireless widgets". For the most part, all the code to manage these two classes of widgets will be identical. The only differing part are some of the underlying field names in each respective db table (table: wired_widgets vs. wireless_widgets). In an effort to reduce code repetition and take advantage of db table reflection, I want to share the same code base and just change which table is being used. I envision a select menu in the app to toggle between the ''wired_widgets'' and ''wireless_widgets'' tables. And subsequently the reflective view,edit,create pages would reflect the slightly differing db fields. Does anyone know of a clever way to do such a thing within rails? Thank you for any help. Matt -- Matt C. Wagner Information Security Analyst Network Intrusion Detection Security Operations Center Corporate Information Security Wells Fargo Bank
Why not just make "wired" and "wireless" widgets properties of the widget if they contain the same info? On 1/3/06, Matt.C.Wagner@wellsfargo.com <Matt.C.Wagner@wellsfargo.com> wrote:> > Hi, > > I want to know if my idea is possible within the rails framework. > > I want to write a rails app that uses the same code to manage two > classes of widgets; "wired widgets" and "wireless widgets". > > For the most part, all the code to manage these two classes of widgets > will be identical. The only differing part are some of the underlying > field names in each respective db table (table: wired_widgets vs. > wireless_widgets). > > In an effort to reduce code repetition and take advantage of db table > reflection, I want to share the same code base and just change which > table is being used. > > I envision a select menu in the app to toggle between the > ''wired_widgets'' and ''wireless_widgets'' tables. And subsequently the > reflective view,edit,create pages would reflect the slightly differing > db fields. > > Does anyone know of a clever way to do such a thing within rails? > > Thank you for any help. > > Matt > > -- > Matt C. Wagner > Information Security Analyst > > Network Intrusion Detection > Security Operations Center > Corporate Information Security > Wells Fargo Bank > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Steven Ross web application & interface developer http://www.zerium.com [phone] 404-488-4364 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060103/366af2fe/attachment.html
Hi Matt, You can use the Single Table Inheritance pattern w/ this one: Database: table "widgets" (w/ all wired and unwired fields, plus "type" column) Models: "WiredWidget < Widget", "UnwiredWidget < Widget" and "Widget < ActiveRecord::Base" Since STI has all fields for all "children" tables, your UnwiredWidget model will have the same fields as WiredWidget ones. A quick-and-dirty solution is to override the column_names method for ActiveRecord. So, you can have something like this: class Widget < ActiveRecord::Base def column_names ["some field", "other field", ...] if type == "WiredWidget" ["some unwired field", "other unwired field", ...] if type == "UnwiredWidget" end end -- juca Matt.C.Wagner@wellsfargo.com wrote:>Hi, > >I want to know if my idea is possible within the rails framework. > >I want to write a rails app that uses the same code to manage two >classes of widgets; "wired widgets" and "wireless widgets". > >For the most part, all the code to manage these two classes of widgets >will be identical. The only differing part are some of the underlying >field names in each respective db table (table: wired_widgets vs. >wireless_widgets). > >In an effort to reduce code repetition and take advantage of db table >reflection, I want to share the same code base and just change which >table is being used. > >I envision a select menu in the app to toggle between the >''wired_widgets'' and ''wireless_widgets'' tables. And subsequently the >reflective view,edit,create pages would reflect the slightly differing >db fields. > >Does anyone know of a clever way to do such a thing within rails? > >Thank you for any help. > > Matt > > >
Matt.C.Wagner@wellsfargo.com
2006-Jan-03 17:55 UTC
[Rails] Changing tables within rails app
Steven Ross wrote:> Why not just make "wired" and "wireless" widgets properties of the > widget if they contain the same info?They don''t contain all the same info. It''s similar info, but will require different and or additional field names and such. For instance, the wireless_widget table might contain an SSID field, whereas the wired_widget table does not. This kind of difference doesn''t matter to the reflective new/edit/list views. I just discovered that changing the table with ''set_table_name'' works how I want. Is this the best way to do it? Matt -- Matt C. Wagner Information Security Analyst Network Intrusion Detection Security Operations Center Corporate Information Security Wells Fargo Bank