Complex fields_for mapping to multi-level model association

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)

def existing_guarantor_attributes=(guarantor_attributes)


class Guarantor < ActiveRecord::Base
belongs_to :loan, :class_name => “Loan”, :foreign_key =>
has_one :personal_detail, :dependent => :destroy

def personal_detail_attributes=(attributes)


class PersonalDetail < ActiveRecord::Base
belongs_to :guarantor, :class_name => “Guarantor”, :foreign_key =>





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

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
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” =>
“middlename”=>"", “surname”=>“Bloggs”}]}]},
{“new_guarantor_attributes” =>
“middlename”=>"", “surname”=>“Smith”}]}]}]

Here’s the results if I use this config “loan[new_guarantor_attributes]
1: firstname: Bill, :lastname: Bloggs
2: firstname: John, :lastname: Smith
{“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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs