Mik-die/mongoid_globalize — Issue #3: .build and save results in nil attributes
Author: brentkirby
Created: 2011-12-13 06:08:25+00:00
State: open

I'm having some weird issues using nested attributes and building embedded models.

When using .build and specifying attributes, on save the embedded model creates an embedded translation doc, with the proper locale set. However, all of its attributes are blank.

For instance, I have a page model, and an embedded Content model. Content is subclassed (perhaps thats the culprit?) with a model called TextBlock which has a "body" attribute.

```ruby
page.contents.build({ body: "some content" }, TextBlock)
# misc code here
page.save
```

Upon save inspecting the embedded TextBlock I have one embedded translation for :en (default locale) but its body, as well as the body on the model itself are both blank.

I'm wondering if its something to do with the changes mongoid made recently with how embedded docs persist mentioned [here](http://mongoid.org/docs/upgrading.html) because if I re-save the embedded doc after the parent is saved, the attributes/translations are properly saved

```ruby
class Page
after_save :resave_contents

def resave_contents
self.contents.each(&:save)
end
end

page.contents.first.inspect #=>

```

All translations are also set properly.

In addition, as mentioned in the mongoid link above, when updating embedded docs with update_attributes, all attrs/translations are updated perfectly, but using `model.attributes=` and `model.write_attribute(key, value)` or `model.write_attribute(key, value, :locale => I18n.locale)` don't seem to persist either.

I tried overriding the nested_attributes callback (contents_attributes=) and the documents update with the exception of translated attributes. Going through the code also got me to try doc.globalize.write(locale, key, value) but the updated attrs still don't persist.

Without translation enabled everything saves properly, and I'm wondering if this isn't an issue created with how mongoid handles setting embedded docs on the parent as of version 2.2.0. When creating/updating non-embedded docs everything works as expected.

I'm happy to fork and test a few things out, I was just wondering if anyone had any thoughts :)