Note that there are some explanatory texts on larger screens.

plurals
  1. POmongoid polymorphic association error
    primarykey
    data
    text
    <p>I'm having some problems using mongoid-3.0.6 with polymorphic fields. Using rails 3.2.8 and ruby 1.9.3</p> <p>Using a normal polymorphic relation:</p> <pre><code>class Shirt include Mongoid::Document field :name, localize: true belongs_to :itemizable, polymorphic: true end class Item include Mongoid::Document field :price, type: Float field :quantity, type: Integer, :default =&gt; 1 has_one :product, as: :itemizable accepts_nested_attributes_for :product end </code></pre> <p>The same association is available through the metadata:</p> <pre><code>&gt;&gt; Item.reflect_on_association(:product) #&lt;Mongoid::Relations::Metadata   autobuild: false,   class_name: Product,   cyclic: nil,   dependent: nil,   inverse_of: nil,   key: _id,   macro: has_one,   name: product,   order: nil,   polymorphic: true,   relation: Mongoid::Relations::Referenced::One,   setter: product=,   versioned: false&gt; &gt;&gt; item = Item.new #&lt;Item _id: 50606c1668ce87692e000003, _type: nil, created_at: nil, updated_at: nil, deleted_at: nil, price: nil, quantity: 1&gt; </code></pre> <p>but when i run</p> <pre><code>&gt;&gt; item.product = Shirt.new or &gt;&gt; item.build_product </code></pre> <p>i got always the same error </p> <pre><code>NameError: uninitialized constant Product </code></pre> <p><a href="https://gist.github.com/3776248" rel="nofollow">Full stack error</a></p> <p>Any thoughts? </p> <p>Thanks in advance. </p> <p>Solved</p> <ul> <li>Found the motive</li> </ul> <p>Need to add the class_name to the relation</p> <pre><code>has_one :product, as: :itemizable, class_name: "Toy" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload