I need untyped associations


#1

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.


#2

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


#3

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

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.


#4

Surely STI is all you need for this?


Alex


#5

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.


#6

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 wrote:

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


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


#7

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.


#8

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


#9

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

On 1/12/06, Liquid 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 - modern e-commerce software
http://typo.leetsoft.com - Open source weblog engine
http://blog.leetsoft.com - Technical weblog