How to use Single Table Inheritance with Modules

So, I have a table…


id int
type varchar(255)
data text

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

class Event < ActiveRecord::Base

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

new_event = Component::FooBarEvent.create :data => {“msg” =>“Hello
assert( new_event == Event.find_by_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

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


How about either

renaming Component::FooBarEvent to ComponentEvents::FooBarEvent


renaming the directory component_events to component ?


On 5/9/07, [email protected] [email protected] wrote:

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

Best regards,
Yuri L.

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

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