mongoid/mongoid — pull request #3709 by TheNaoX (2014-06-26 20:51:00+00:00)
Title: (uninitialized constant for polymorphic) Mongoid does not specify the target class when using nested attributes

Hi guys!

I've been experiencing this issue when using nested attributes and polymorphic associations, the issue is described as follows:

``` ruby
class Product
include Mongoid::Document
belongs_to :shop, polymorphic: true
accepts_nested_attributes_for :shop, autosave: true
end

class ShopOne
include Mongoid::Document

field :something, type: String

has_one :product, as: :shop
end

class ShopTwo
include Mongoid::Document

field :another_thing, type: Integer

has_one :product, as: :shop
end
```

And when I do this
``` ruby
Product.new(shop_type: "ShopOne", shop_attributes: { something: 'Foo' })
```

It gives me this
```ruby
uninitialized constant Shop
{... path}/mongoid/relations/builders/nested_attributes/one.rb:32
```

The solution I'm proposing is to override the class_name attribute inside of the metadata class only if the association you're trying to store is polymorphic (See code in the pull request).