Forum: Ruby on Rails I need untyped associations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sorenson (Guest)
on 2006-01-08 16:54
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.
Gregory Seidman (Guest)
on 2006-01-08 17:00
(Received via mailing list)
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
Joshua Susser (Guest)
on 2006-01-08 17:46
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/...

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.
Alex Young (Guest)
on 2006-01-09 11:28
(Received via mailing list)
Surely STI is all you need for this?

--
Alex
Sorenson (Guest)
on 2006-01-13 01:45
Alex Young 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.
Tobias Luetke (Guest)
on 2006-01-13 02:27
(Received via mailing list)
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 <jsorenson@bellsouth.net> 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
739e2894aaaa2e2da1627dd5d819fba7?d=identicon&s=25 Daniel -. (liquid)
on 2006-01-13 03:39
(Received via mailing list)
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
Sorenson (Guest)
on 2006-01-13 04:46
This looks promising...

Tobias Luetke 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.
......
Tobias Luetke (Guest)
on 2006-01-13 15:07
(Received via mailing list)
Hey I never got an email back from a library I released!
> On 1/12/06, Liquid <has.sox@gmail.com> 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
This topic is locked and can not be replied to.