justinfrench/formtastic — Issue #751: nested forms not compatible with mongo_mapper
Author: jjoe64
Created: 2011-12-05 08:46:44+00:00
State: open

if have a one-to-many relation in my model:

```
class Office
include MongoMapper::Document
many :journey_descriptions
accepts_nested_attributes_for :journey_descriptions
end

class JourneyDescription
include MongoMapper::EmbeddedDocument
end
```

In the view I generate a nested form:
```
= form.inputs :name => t("offices.journey_description")+" %i", :for => :journey_descriptions do |jd_form|
= jd_form.input :title
```

The form looks good, but when I want to save, this error comes up:

can't convert String into Integer

### Stacktrace:
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/keys.rb:69:in `[]'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/keys.rb:69:in `load'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb:16:in `block in find_target'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb:15:in `map'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb:15:in `find_target'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/associations/proxy.rb:113:in `load_target'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/associations/proxy.rb:94:in `send'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/embedded_callbacks.rb:18:in `block in run_callbacks'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/embedded_callbacks.rb:17:in `each'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/embedded_callbacks.rb:17:in `run_callbacks'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/validations.rb:86:in `run_validations!'
> activemodel (3.1.1.rc1) lib/active_model/validations.rb:179:in `valid?'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/validations.rb:28:in `valid?'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/validations.rb:23:in `save'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/keys.rb:220:in `update_attributes'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/accessible.rb:23:in `update_attributes'
> mongo_mapper (0.9.2) lib/mongo_mapper/plugins/protected.rb:32:in `update_attributes'
> app/controllers/offices_controller.rb:72:in `block in update'
> actionpack (3.1.1.rc1) lib/action_controller/metal/mime_responds.rb:269:in `call'
> actionpack (3.1.1.rc1) lib/action_controller/metal/mime_responds.rb:269:in `retrieve_response_from_mimes'
> actionpack (3.1.1.rc1) lib/action_controller/metal/mime_responds.rb:194:in `respond_to'
> app/controllers/offices_controller.rb:71:in `update'

## Workaround
I found out a workaround for this issue: You have to convert the journey_descriptions_attributes Hash to a Array.
`params[:office][:journey_descriptions_attributes] = params[:office][:journey_descriptions_attributes].values`