# Execute code when an inheritance happens, or disabling STI

**URL:** <https://www.ruby-forum.com/t/execute-code-when-an-inheritance-happens-or-disabling-sti/99868>\
**Category:** Rails\
**Created:** [July 26, 2006, 1:04am UTC](https://www.ruby-forum.com/t/execute-code-when-an-inheritance-happens-or-disabling-sti/99868 "2006-07-26T01:04:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [July 26, 2006, 1:04am UTC](https://www.ruby-forum.com/t/execute-code-when-an-inheritance-happens-or-disabling-sti/99868/1 "2006-07-26T01:04:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [July 26, 2006, 6:11am UTC](https://www.ruby-forum.com/t/execute-code-when-an-inheritance-happens-or-disabling-sti/99868/2 "2006-07-26T06:11:10Z")

</div>

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

> end  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](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](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [July 26, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/execute-code-when-an-inheritance-happens-or-disabling-sti/99868/3 "2006-07-26T18:54:40Z")

</div>

Kevin O. wrote:

> On Wednesday, July 26, 2006, at 1:04 AM, Alex W. wrote:
> 
> > end  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](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](http://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.

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [July 27, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/execute-code-when-an-inheritance-happens-or-disabling-sti/99868/4 "2006-07-27T20:05:51Z")

</div>

> 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](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

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [July 27, 2006, 6:55pm UTC](https://www.ruby-forum.com/t/execute-code-when-an-inheritance-happens-or-disabling-sti/99868/5 "2006-07-27T18:55:59Z")

</div>

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](http://somethinglearned.com)
