The Cool Web Guy
2008-May-11 07:24 UTC
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) 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 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) -%> <div class="guarantor"> <%= f.text_field :firstname %> <%= f.text_field :middlename %> <%= f.text_field :surname %> <%= link_to_function "Remove", ... %> </div> <% 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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---