Forum: Ruby on Rails polymorphic hm=>t

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.
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-28 05:31
(Received via mailing list)
Hi,

I have the following class architecture:

class Container < ActiveRecord::Base
  has_many :containments, :order => 'position'
  has_many :parent_containments, :as => :containable, :class_name =>
'Containment'
  has_many :containable_items, :through => :containments, :source
=> :containable
end

class Containment < ActiveRecord::Base
  belongs_to :container
  belongs_to :containable, :polymorphic => true
end

class ContainedThing < ActiveRecord::Base
  has_many :parent_containments, :as => :containable, :class_name =>
'Containment'
  has_many :containers, :through => :parent_containments
end

class OtherContainedThing < ActiveRecord::Base
  has_many :parent_containments, :as => :containable, :class_name =>
'Containment'
  has_many :containers, :through => :parent_containments
end

The logic is... A container can contain many items. These items may be
being contained inside other containers as well (ie has_and_belongs_to
or has_many :through type association). Also, it's possible for a
container to store other containers in itself (hence the
parent_containments association inside the container class).

Rails doesn't like my code very much, though.

I can't use the containable_items has_many :through association
because Rails complains that I can't use a polymorphic association as
the target of a has_many :through.

Is there something I'm missing, or do I have to do most of the work
via hand here?

(ie no automatic methods, etc, and I have to build, destroy and
retrieve objects with a series of lines of code rather than simple one-
method invocations, etc.)?

Thanks heaps,
Julian.
This topic is locked and can not be replied to.