Forum: Ruby on Rails polymorphic hm=>t

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Julian L. (Guest)
on 2009-02-28 06:31
(Received via mailing list)

I have the following class architecture:

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

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

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

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

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,
This topic is locked and can not be replied to.