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.