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

**URL:** <https://www.ruby-forum.com/t/can-i-fake-an-object-model-on-top-of-a-flat-activerecord/158875>\
**Category:** Rails\
**Created:** [January 29, 2009, 12:40am UTC](https://www.ruby-forum.com/t/can-i-fake-an-object-model-on-top-of-a-flat-activerecord/158875 "2009-01-29T00:40:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [January 29, 2009, 12:40am UTC](https://www.ruby-forum.com/t/can-i-fake-an-object-model-on-top-of-a-flat-activerecord/158875/1 "2009-01-29T00:40:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [January 29, 2009, 12:54am UTC](https://www.ruby-forum.com/t/can-i-fake-an-object-model-on-top-of-a-flat-activerecord/158875/2 "2009-01-29T00:54:38Z")

</div>

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](http://wiki.rubyonrails.org/rails/pages/singletableinheritance)

Good luck!  
Tim

On Jan 28, 3:40 pm, Duane M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [January 29, 2009, 12:55am UTC](https://www.ruby-forum.com/t/can-i-fake-an-object-model-on-top-of-a-flat-activerecord/158875/3 "2009-01-29T00:55:58Z")

</div>

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.
