Can I "fake" an object model on top of a flat ActiveRecord?


#1

This might be a painfully bad idea, but it’s gotten me curious. I have
a generic object Node, which has a name attribute. Say for example that
one of the names is “Widget”.

What I’m wondering is, is there a way to manipulate ActiveRecord to make
a class named Widget, that behaves exactly like a Node with the only
difference being that all operations only work on the condition
(name=“Widget”) is true? I can do a simple class Widget < Node to
get it started, but I’m looking for what the next line would be, where I
can specify the global condition. In all other cases,

History : The database itself is generated rather, well, generically by
scanning a file where I don’t know the nature of the content beyond
simple node relationships (i.e. I don’t know ahead of time all values
for “name”). However, I’m thinking that after the flat data storage
portion is taken care of I could go in and maybe write some sort of
generator for myself that would build up the Ruby classes to deal with
the database, and hide the whole thing.


#2

I’m not completely clear on what your trying to accomplish but it
sounds like STI may be what your looking for?

http://wiki.rubyonrails.org/rails/pages/singletableinheritance

Good luck!
Tim

On Jan 28, 3:40 pm, Duane M. removed_email_address@domain.invalid


#3

You know, that might be exactly it. I knew I’d heard it somewhere
before :).

Thanks! Sorry for the interruption.

D
Duane M. wrote:

This might be a painfully bad idea, but it’s gotten me curious. I have
a generic object Node, which has a name attribute. Say for example that
one of the names is “Widget”.

What I’m wondering is, is there a way to manipulate ActiveRecord to make
a class named Widget, that behaves exactly like a Node with the only
difference being that all operations only work on the condition
(name=“Widget”) is true? I can do a simple class Widget < Node to
get it started, but I’m looking for what the next line would be, where I
can specify the global condition. In all other cases,

History : The database itself is generated rather, well, generically by
scanning a file where I don’t know the nature of the content beyond
simple node relationships (i.e. I don’t know ahead of time all values
for “name”). However, I’m thinking that after the flat data storage
portion is taken care of I could go in and maybe write some sort of
generator for myself that would build up the Ruby classes to deal with
the database, and hide the whole thing.