# Complex fields\_for mapping to multi-level model association

**URL:** <https://www.ruby-forum.com/t/complex-fields-for-mapping-to-multi-level-model-association/137276>\
**Category:** Rails\
**Created:** [May 11, 2008, 9:25am UTC](https://www.ruby-forum.com/t/complex-fields-for-mapping-to-multi-level-model-association/137276 "2008-05-11T09:25:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![The\_Cool\_Web\_Guy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbe5ce/32.png) [@The\_Cool\_Web\_Guy](https://www.ruby-forum.com/u/The_Cool_Web_Guy)\
**Post date:** [May 11, 2008, 9:25am UTC](https://www.ruby-forum.com/t/complex-fields-for-mapping-to-multi-level-model-association/137276/1 "2008-05-11T09:25:04Z")

</div>

I’ve come across what might be a limitation of rails, or I’ve missed  
something completely

Here’s the situation:

I have 3 models as follows

class Loan \< ActiveRecord::Base  
has\_many :guarantors, :class\_name =\> “Guarantor”, :dependent  
=\> :destroy

```
def new_guarantor_attributes=(guarantor_attributes)
  guarantors.build(guarantor_attributes)
end

def existing_guarantor_attributes=(guarantor_attributes)
  ...
end

```

end

class Guarantor \< ActiveRecord::Base  
belongs\_to :loan, :class\_name =\> “Loan”, :foreign\_key =\>  
“loan\_id”  
has\_one :personal\_detail, :dependent =\> :destroy

```
def personal_detail_attributes=(attributes)
  build_personal_detail(attributes)
end

```

end

class PersonalDetail \< ActiveRecord::Base  
belongs\_to :guarantor, :class\_name =\> “Guarantor”, :foreign\_key =\>  
“guarantor\_id”

# firstname:string

# middlename:string

# surname:string

end

All the associations work fine, but populating them does not, in my  
form I have a repeating element generated using javascript, similar to  
the tasks example found in [#75 Complex Forms Part 3 - RailsCasts](http://railscasts.com/episodes/75)

The partial I am repeating looks something like this  
\<% fields\_for(“loan[new\_guarantor\_attributes][]  
[personal\_detail\_attributes]”, object, &block) -%\>

 \<%= f.text\_field :firstname %\> \<%= f.text\_field :middlename %\> \<%= f.text\_field :surname %\>

```
<%= link_to_function "Remove", ... %>

```

\<% end -%\>

It works fine if I’m only saving attributes of the guarantor, but its  
fails because no matter which configuration I put the first parameter  
of fields\_for, it either passes an incomplete array, a jumbled array  
or in the example above, it passes and empty array which looks like  
this:  
[{“personal\_detail\_attributes”=\>[]},  
{“personal\_detail\_attributes”=\>[]},  
{“personal\_detail\_attributes”=\>[]}]  
As you can see it does not include any of the text\_fields

I would have thought that would pass an array of guarantors each with  
a single personal\_detail, something like this:  
[{“new\_guarantor\_attributes” =\>  
[{“personal\_detail\_attributes”=\>[{“firstname”=\>“Bill”,  
“middlename”=\>“”, “surname”=\>“Bloggs”}]}]},  
{“new\_guarantor\_attributes” =\>  
[{“personal\_detail\_attributes”=\>[{“firstname”=\>“John”,  
“middlename”=\>“”, “surname”=\>“Smith”}]}]}]

Here’s the results if I use this config “loan[new\_guarantor\_attributes]  
[personal\_detail\_attributes][]”  
Inputs:  
1: firstname: Bill, :lastname: Bloggs  
2: firstname: John, :lastname: Smith  
{“personal\_detail\_attributes”=\>[{“firstname”=\>“Bill”},  
{“middlename”=\>“”, “firstname”=\>“John”}, {“middlename”=\>“”,  
“surname”=\>“Bloggs”}, {“surname”=\>“Smith”}]}

As you can see the results are jumpled.

Anyway, to the point, here’s how I need it to work:

Iterate through each guarantor in guarantor\_attributes and build it,  
and at the same time build each personal\_detail (one per guarantor)

Any ideas?
