# Activerecord associations like class structure

**URL:** <https://www.ruby-forum.com/t/activerecord-associations-like-class-structure/167405>\
**Category:** Ruby\
**Created:** [May 7, 2009, 2:47pm UTC](https://www.ruby-forum.com/t/activerecord-associations-like-class-structure/167405 "2009-05-07T14:47:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [May 7, 2009, 2:47pm UTC](https://www.ruby-forum.com/t/activerecord-associations-like-class-structure/167405/1 "2009-05-07T14:47:08Z")

</div>

Hi,

This question is based on another thread that helped me realize the  
initial idea I had about a library format was not good.

How is it possible to mimic an ActiveRecord association like class  
structure?

Something like:  
Module Something  
Class Main  
def connection  
end  
end

Class Resource \< Main  
def method  
call\_resource  
end  
end  
end

and being able to create something like  
con = Main.new(user, pass)  
result = con.resources.method

Thanks.

Sorry for opening another thread about this.

---

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [May 8, 2009, 1:19am UTC](https://www.ruby-forum.com/t/activerecord-associations-like-class-structure/167405/2 "2009-05-08T01:19:36Z")

</div>

On Thu, May 7, 2009 at 5:46 AM, Marcelo B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Â Class Main
> 
> and being able to create something like  
> con = Main.new(user, pass)  
> result = con.resources.method
> 
> Thanks.
> 
> Sorry for opening another thread about this.

One way (and one that lets you either use the default connection on  
the base class or override it per sub-class) would be something like:

module Generic  
class Base  
def self.connect(resource, opts)  
@connection = resource.connect(opts)  
end

```
def connection
  @connection || (self!=Generic::Base && Generic::Base.connection)
end

```

end  
end

class Resource \< Generic::Base

# whatever

end
