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?

On Wednesday, July 26, 2006, at 1:04 AM, Alex W. wrote:

end
[email protected]
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.com

Kevin O. wrote:

On Wednesday, July 26, 2006, at 1:04 AM, Alex W. wrote:

end
[email protected]
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.com

No 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.

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 S.
http://somethinglearned.com

Awesome. 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

On 25-Jul-06, at 4:04 PM, Alex W. wrote:

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 S.
http://somethinglearned.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs