mongoid/mongoid — Issue #1806: find_or_create_by cannot serialize an object into BSON
Author: sebastianmaier
Created: 2012-03-08 22:51:22+00:00
State: open

If I use find_or_create_by in this way I get an error:

```
class Page
include Mongoid::Document

field :title, :type => String

validates :title, :uniqueness => true

has_many :contentElements, as: :contentScope

end

class ContentElement
include Mongoid::Document

field :name, :type => String

validates :name, :uniqueness => true

belongs_to :contentScope, :polymorphic => true
end
```

This one fails:

```
page = Page.create title: "xyz"
ContentElement.find_or_create_by(name: "notexsisting", contentScope: page)
```
error message is:
BSON::InvalidDocument: Cannot serialize an object of class Page into BSON.

but this is fine:

```
page = Page.create title: "xyz"
ContentElement.create(name: "notexsisting", contentScope: page)
```