Alex Wayne
2006-Jul-26 01:10 UTC
[Rails] Execute code when an inheritance happens, or disabling STI
I am trying to inherit from a ActiveRecord class defined in a plugin. The problem is that ActiveRecord thinks I am doing an STI and is looking up a table that doesn''t exist. Currently, I have a method in the parent class that looks like this: def self.fix_table_name class_eval do set_table_name(Inflector.tableize(self.to_s)) end end The problem is that this method has to be manually called from each inheriting class. There must be a better way to execute this code. Any ideas? -- Posted via http://www.ruby-forum.com/.
Kevin Olbrich
2006-Jul-26 04:09 UTC
[Rails] Execute code when an inheritance happens, or disabling STI
On Wednesday, July 26, 2006, at 1:04 AM, Alex Wayne wrote:>I am trying to inherit from a ActiveRecord class defined in a plugin. >The problem is that ActiveRecord thinks I am doing an STI and is looking >up a table that doesn''t exist. > >Currently, I have a method in the parent class that looks like this: > > def self.fix_table_name > class_eval do > set_table_name(Inflector.tableize(self.to_s)) > end > end > >The problem is that this method has to be manually called from each >inheriting class. There must be a better way to execute this code. > >Any ideas? > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsDo you have a ''type'' column in your table? If so, that is probably confusing AR. _Kevin www.sciwerks.com -- Posted with http://DevLists.com. Sign up and save your mailbox.
Alex Wayne
2006-Jul-26 17:01 UTC
[Rails] Re: Execute code when an inheritance happens, or disabling S
Kevin Olbrich wrote:> On Wednesday, July 26, 2006, at 1:04 AM, Alex Wayne wrote: >> end >>Rails@lists.rubyonrails.org >>http://lists.rubyonrails.org/mailman/listinfo/rails > > Do you have a ''type'' column in your table? If so, that is probably > confusing AR. > > _Kevin > www.sciwerks.comNo type column at all, but without calling the fix_table_name method, it errors because the its getting the table name from the parent class, which does not have a table. -- Posted via http://www.ruby-forum.com/.
Trevor Squires
2006-Jul-27 16:53 UTC
[Rails] Execute code when an inheritance happens, or disabling STI
On 25-Jul-06, at 4:04 PM, Alex Wayne wrote:> I am trying to inherit from a ActiveRecord class defined in a plugin. > The problem is that ActiveRecord thinks I am doing an STI and is > looking > up a table that doesn''t exist. > > Currently, I have a method in the parent class that looks like this: > > def self.fix_table_name > class_eval do > set_table_name(Inflector.tableize(self.to_s)) > end > end > > The problem is that this method has to be manually called from each > inheriting class. There must be a better way to execute this code. > > Any ideas? >Alex, I''m assuming that you''re the author of the plugin trying to figure out how to make a usage pattern of "just inherit from PluginClass" work. If the ActiveRecord class defined in your plugin is *only* ever meant to be an abstract base class then the simplest solution is to tell ActiveRecord that the class is abstract: class ThePluginClass < ActiveRecord::Base self.abstract_class = true end If your plugin also needs to supply a concrete instance then I''d also supply a ConcreteClass < ThePluginClass with the plugin. Hope this helps, Trevor -- Trevor Squires http://somethinglearned.com
Alex Wayne
2006-Jul-27 18:06 UTC
[Rails] Re: Execute code when an inheritance happens, or disabling S
> Alex, > > I''m assuming that you''re the author of the plugin trying to figure > out how to make a usage pattern of "just inherit from PluginClass" work. > > If the ActiveRecord class defined in your plugin is *only* ever meant > to be an abstract base class then the simplest solution is to tell > ActiveRecord that the class is abstract: > > class ThePluginClass < ActiveRecord::Base > self.abstract_class = true > end > > If your plugin also needs to supply a concrete instance then I''d also > supply a ConcreteClass < ThePluginClass with the plugin. > > Hope this helps, > Trevor > -- > Trevor Squires > http://somethinglearned.comAwesome. You absolutely rock. It worked perfectly. And yes it is my plugin, and I am just trying to get the cleanest possible interface for it. Thanks again! -Alex -- Posted via http://www.ruby-forum.com/.