# How to use Single Table Inheritance with Modules

**URL:** <https://www.ruby-forum.com/t/how-to-use-single-table-inheritance-with-modules/103752>\
**Category:** Rails\
**Created:** [May 9, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/how-to-use-single-table-inheritance-with-modules/103752 "2007-05-09T22:28:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bramski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c2a13f/32.png) [@bramski](https://www.ruby-forum.com/u/bramski)\
**Post date:** [May 9, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/how-to-use-single-table-inheritance-with-modules/103752/1 "2007-05-09T22:28:50Z")

</div>

So, I have a table…

# events

id int  
type varchar(255)  
data text

Which I’m using to log some simple system events…

class Event \< ActiveRecord::Base  
end

I’d like to file quite a lot of events in here, and load them later  
into a general parsing and display system for the events. However,  
what I’d really like to do is:

class Component::FooBarEvent \< ActiveRecord::Base  
end

new\_event = Component::FooBarEvent.create :data =\> {“msg” =\>“Hello  
world”}  
assert( new\_event == Event.find\_by\_id(new\_event.id) )

instead what I get is…  
LoadError: Expected /home/bramski/source/trunk/config/…/app/models/  
events/component\_events/foo\_bar\_event.rb to define FooBarEvent

If I look in the SQL I see…  
SQL (0.000269) INSERT INTO events ( `type`, `data`,  
`eventable1_type`, `foreign_id`) VALUES(‘TestEvent’, ‘— \nmsg:  
Hello world\n’)

Now… I should never see the above LoadError for the missing constant  
as Component::FooBarEvent is already defined. What is confusing here  
is, why is rails putting in the unqualified constant for the object  
(FooBarEvent) as opposed to the fully qualified constant name  
(Component::FooBarEvent).

Is there a way to get rails to store and load Component::FooBarEvent  
as the type, rather than just FooBarEvent?

---

<div class="post-metadata">

**Author:** ![bramski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c2a13f/32.png) [@bramski](https://www.ruby-forum.com/u/bramski)\
**Post date:** [May 10, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/how-to-use-single-table-inheritance-with-modules/103752/2 "2007-05-10T13:13:58Z")

</div>

Hey,

How about either

renaming Component::FooBarEvent to ComponentEvents::FooBarEvent

or

renaming the directory component\_events to component ?

Cheers,  
Yuri

On 5/9/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> new\_event = Component::FooBarEvent.create :data =\> {“msg” =\>"Hello  
> Hello world\n’)
> 
> >

–  
Best regards,  
Yuri L.

---

<div class="post-metadata">

**Author:** ![bramski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c2a13f/32.png) [@bramski](https://www.ruby-forum.com/u/bramski)\
**Post date:** [May 11, 2007, 2:18am UTC](https://www.ruby-forum.com/t/how-to-use-single-table-inheritance-with-modules/103752/3 "2007-05-11T02:18:51Z")

</div>

That doesn’t really do anything, I still don’t get ActiveRecord  
storing as “type” =\> “ComponentEvents::FooBarEvent”, I get “type” =\>  
“FooBarEvent”, and rails is unable to locate the class  
“Object::FooBarEvent” because it’s “ComponentEvents::FooBarEvent”.
