# I need untyped associations

**URL:** <https://www.ruby-forum.com/t/i-need-untyped-associations/51876>\
**Category:** Rails\
**Created:** [January 8, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/i-need-untyped-associations/51876 "2006-01-08T16:54:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sorenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@sorenson](https://www.ruby-forum.com/u/sorenson)\
**Post date:** [January 8, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/i-need-untyped-associations/51876/1 "2006-01-08T16:54:10Z")

</div>

I am in the process of trying to migrate to ROR from a home grown ORM,  
but one stumbling block is ActiveRecord’s typed associations and object  
ID assignment scheme. In the home grown system, I have a master table  
which _all_ objects are packed into and a master associative table which  
holds _all_ associations. This allows each object, regardless of type,  
to have a unique ID and thus it is easy to allow any object to associate  
with any other object regardless of type. This is handy for things such  
as Folder objects which should be able to contain objects of arbitrary  
type. In ActiveState, it appears that ID’s are only unique within a  
particular table and associations require explicit type information (to  
refer to a particular table). So folder.contents can’t really refer to  
an arbitrary collection of objects. I’m sure this has come up within  
the ROR community before and there is probably an easy solution I’m  
overlooking.

---

<div class="post-metadata">

**Author:** ![sorenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@sorenson](https://www.ruby-forum.com/u/sorenson)\
**Post date:** [January 8, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/i-need-untyped-associations/51876/2 "2006-01-08T17:00:42Z")

</div>

On Sun, Jan 08, 2006 at 04:54:10PM +0100, Sorenson wrote:  
} I am in the process of trying to migrate to ROR from a home grown ORM,  
} but one stumbling block is ActiveRecord’s typed associations and  
object  
} ID assignment scheme. In the home grown system, I have a master  
table  
} which _all_ objects are packed into and a master associative table  
which  
} holds _all_ associations. This allows each object, regardless of  
type,  
} to have a unique ID and thus it is easy to allow any object to  
associate  
} with any other object regardless of type. This is handy for things  
such  
} as Folder objects which should be able to contain objects of arbitrary  
} type. In ActiveState, it appears that ID’s are only unique within a  
} particular table and associations require explicit type information  
(to  
} refer to a particular table). So folder.contents can’t really refer  
to  
} an arbitrary collection of objects. I’m sure this has come up within  
} the ROR community before and there is probably an easy solution I’m  
} overlooking.

The simplest thing is to create views in the DB, and triggers on the  
views  
for inserting/updating, I expect.

–Greg

---

<div class="post-metadata">

**Author:** ![sorenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@sorenson](https://www.ruby-forum.com/u/sorenson)\
**Post date:** [January 8, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/i-need-untyped-associations/51876/3 "2006-01-08T17:46:10Z")

</div>

Polymorphic associations are on their way. DHH has added “preliminary  
support” in edgerails (from the changelog). I don’t know what the  
performance impact would be for doing the whole db that way, but at  
least it will be along soon as an option. You can see a mention of this  
in here (the presentation is worth checking out):  
[http://weblog.rubyonrails.org/articles/2005/12/22/new-37signals-targets-for-rails-extraction](http://weblog.rubyonrails.org/articles/2005/12/22/new-37signals-targets-for-rails-extraction)

Sorenson wrote:

> I am in the process of trying to migrate to ROR from a home grown ORM,  
> but one stumbling block is ActiveRecord’s typed associations and object  
> ID assignment scheme. In the home grown system, I have a master table  
> which _all_ objects are packed into and a master associative table which  
> holds _all_ associations. This allows each object, regardless of type,  
> to have a unique ID and thus it is easy to allow any object to associate  
> with any other object regardless of type. This is handy for things such  
> as Folder objects which should be able to contain objects of arbitrary  
> type. In ActiveState, it appears that ID’s are only unique within a  
> particular table and associations require explicit type information (to  
> refer to a particular table). So folder.contents can’t really refer to  
> an arbitrary collection of objects. I’m sure this has come up within  
> the ROR community before and there is probably an easy solution I’m  
> overlooking.

---

<div class="post-metadata">

**Author:** ![sorenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@sorenson](https://www.ruby-forum.com/u/sorenson)\
**Post date:** [January 9, 2006, 11:28am UTC](https://www.ruby-forum.com/t/i-need-untyped-associations/51876/4 "2006-01-09T11:28:57Z")

</div>

Surely STI is all you need for this?

–  
Alex

---

<div class="post-metadata">

**Author:** ![sorenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@sorenson](https://www.ruby-forum.com/u/sorenson)\
**Post date:** [January 13, 2006, 1:45am UTC](https://www.ruby-forum.com/t/i-need-untyped-associations/51876/5 "2006-01-13T01:45:29Z")

</div>

Alex Y. wrote:

> Surely STI is all you need for this?
> 
> –  
> Alex

I can see how STI would work if there is only one object table. But  
this could get messy after there are more than a few branches on the  
inheritance tree. I may still try it nonetheless.

---

<div class="post-metadata">

**Author:** ![sorenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@sorenson](https://www.ruby-forum.com/u/sorenson)\
**Post date:** [January 13, 2006, 2:27am UTC](https://www.ruby-forum.com/t/i-need-untyped-associations/51876/6 "2006-01-13T02:27:36Z")

</div>

This feature is provided using

belongs\_to :files, :polymorphical =\> true

This requires you to have an additional \_type column. in this case  
file\_type and file\_id.

This belongs\_to link would allow you to point to any other object in  
the system without the need for a separate key registry.

In combination with the new :through associations you can then make a  
link table.

class Folder \< AR:B  
has\_many :links  
has\_many :content, :through =\> :links, :as =\> :object  
end

def Link \< AR:B  
belogns\_to :folder  
belogns\_to :object, :polymorphic =\> true  
end

def RandomObject \< AR:B  
end

f = Folder.create  
ro = RandomObject.create

f.links.create( :object =\> ro )

f.content #=\> [ro]

All those features are only available in trunk and edge rails.

On 1/8/06, Sorenson [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> refer to a particular table). So folder.contents can’t really refer to

–  
Tobi  
[http://jadedpixel.com](http://jadedpixel.com) - modern e-commerce software  
[http://typo.leetsoft.com](http://typo.leetsoft.com) - Open source weblog engine  
[http://blog.leetsoft.com](http://blog.leetsoft.com) - Technical weblog

---

<div class="post-metadata">

**Author:** ![sorenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@sorenson](https://www.ruby-forum.com/u/sorenson)\
**Post date:** [January 13, 2006, 4:46am UTC](https://www.ruby-forum.com/t/i-need-untyped-associations/51876/7 "2006-01-13T04:46:39Z")

</div>

This looks promising…

Tobias L. wrote:

> This feature is provided using
> 
> belongs\_to :files, :polymorphical =\> true
> 
> This requires you to have an additional \_type column. in this case  
> file\_type and file\_id.  
> …

---

<div class="post-metadata">

**Author:** ![sorenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@sorenson](https://www.ruby-forum.com/u/sorenson)\
**Post date:** [January 13, 2006, 3:39am UTC](https://www.ruby-forum.com/t/i-need-untyped-associations/51876/8 "2006-01-13T03:39:59Z")

</div>

Hi Tobias,

This feature looks great… Is there any documentation/tutorials  
written  
for this yet?

I have rails 1.0.0 installed. When are these features likely to be  
included  
into the main gem?

Thanx in advance

Dan

---

<div class="post-metadata">

**Author:** ![sorenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@sorenson](https://www.ruby-forum.com/u/sorenson)\
**Post date:** [January 13, 2006, 3:07pm UTC](https://www.ruby-forum.com/t/i-need-untyped-associations/51876/9 "2006-01-13T15:07:32Z")

</div>

Hey I never got an email back from a library I released!

> On 1/12/06, Liquid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This feature looks great… Is there any documentation/tutorials written  
> for this yet?

I use it heavily in Shopify and David uses it in his current project  
as well so these features are certainly stableish. I don’t think there  
is any documentation at this point but if you download and watch  
David’s great talk at the Ruby and Snakes event he shows several  
slides on the topic

–  
Tobi  
[http://jadedpixel.com](http://jadedpixel.com) - modern e-commerce software  
[http://typo.leetsoft.com](http://typo.leetsoft.com) - Open source weblog engine  
[http://blog.leetsoft.com](http://blog.leetsoft.com) - Technical weblog
