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. -- Posted via http://www.ruby-forum.com/.
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
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.-- Posted via http://www.ruby-forum.com/.
Surely STI is all you need for this? -- Alex Joshua Susser wrote:> 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. > > >
Alex Young wrote:> Surely STI is all you need for this? > > -- > AlexI 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. -- Posted via http://www.ruby-forum.com/.
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:> 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. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://jadedpixel.com - modern e-commerce software http://typo.leetsoft.com - Open source weblog engine http://blog.leetsoft.com - Technical weblog
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 On 1/13/06, Tobias Luetke <tobias.luetke@gmail.com> 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. > > 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: > > 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. > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > Tobi > http://jadedpixel.com - modern e-commerce software > http://typo.leetsoft.com - Open source weblog engine > http://blog.leetsoft.com - Technical weblog > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060113/243320cd/attachment.html
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....... -- Posted via http://www.ruby-forum.com/.
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