I'm working on building an app to keep track of product designs, and I'm having some trouble with my associations. Basically I have a model (Assembly) which needs to have polymorphic association, but also needs to be able to belong to itself.
To illustrate, I have three models: Product, Assembly, and Part.
- A Product can have many Assemblies.
- An Assembly can have many Parts AND Assemblies.
- An Assembly belongs to a Product OR an Assembly.
- A Part belongs to an Assembly.
My model definitions are currently like this:
product.rb
class Product < ActiveRecord::Base
belongs_to :product_family
has_many :assemblies, as: :assemblable
end
assembly.rb
class Assembly < ActiveRecord::Base
belongs_to :assemblable, polymorphic: true
has_many :parts
has_many :subassemblies, as: :assemblable
end
part.rb
class Part < ActiveRecord::Base
belongs_to :assembly
belongs_to :product_family
end
What I would like to be able to do is, given an assembly called "top_assy":
top_assy.subassemblies.create
However, when I try this, I get the following error:
NameError: uninitialized constant Assembly::Subassembly
I'm clearly doing something wrong here - what am I missing? I have already tried adding 'class_name: "Assembly"' as an argument to the 'has_many :subassemblies' command.
Thanks in advance!