title: HABTM Double-Nested fields_for
body: <p>Interesting segment of code that I can't get to work. I have the following models/relationships (unnecessary code excluded)</p>
  
  <pre><code>class Service &lt; ActiveRecord::Base
    belongs_to :service_category, :foreign_key =&gt; "cats_uid_fk"
    belongs_to :service_type, :foreign_key =&gt; "types_uid_fk"
    has_and_belongs_to_many :service_subtypes, :join_table =&gt; "services_to_service_subs"
    belongs_to :service_request, :foreign_key =&gt; "audits_uid_fk"
  
    accepts_nested_attributes_for :service_subtypes
  end
  
  class ServiceSubtype &lt; ActiveRecord::Base
    belongs_to :service_types, :foreign_key =&gt; "types_uid_fk"
    has_and_belongs_to_many :services, :join_table =&gt; "services_to_service_subs"
  end
  </code></pre>
  
  <p>The form displaying all this info:</p>
  
  <pre><code>&lt;% form_for(@request, :url =&gt; { :action =&gt; :create }) do |form| %&gt;
   &lt;table&gt;   
  
  ...other data...
  
   &lt;% form.fields_for :services do |fields| %&gt;
    &lt;%= fields.hidden_field :cats_uid_fk %&gt;
    &lt;%= fields.hidden_field :types_uid_fk %&gt;
    &lt;% fields.fields_for :service_subtypes do |subtype| %&gt;
     &lt;%= subtype.hidden_field :id %&gt;
    &lt;% end %&gt; 
   &lt;% end %&gt;   
  
   &lt;p&gt;
     &lt;%= form.submit "Create", :class=&gt;"hargray" %&gt;
   &lt;/p&gt;         
  &lt;% end %&gt; 
  </code></pre>
  
  <p>And the controller to handle the submit:</p>
  
  <pre><code>def create
   logger.debug params[:service_request].inspect
  
   @request = ServiceRequest.new(params[:service_request])
   if session[:cus_id]
    @request.customer = Customer.find session[:cus_id]
   end
  
   begin      
    @request.save!
    flash[:notice] = "Information submitted successfully. You will be contacted by a customer service representative regarding the services you selected."
    redirect_to :controller =&gt; "customer", :action =&gt; "index"
   rescue Exception =&gt; exc
    flash[:notice] = "#{ format_validations(@request) } - #{exc.message}"
    render :action =&gt; "new"
   end
  
  end
  </code></pre>
  
  <p>The html looks clean:</p>
  
  <pre><code>&lt;input id="service_request_services_attributes_0_cats_uid_fk" name="service_request[services_attributes][0][cats_uid_fk]" type="hidden" value="1" /&gt;
    &lt;input id="service_request_services_attributes_0_types_uid_fk" name="service_request[services_attributes][0][types_uid_fk]" type="hidden" value="1" /&gt;
    &lt;input id="service_request_services_attributes_0_service_subtypes_attributes_0_id" name="service_request[services_attributes][0][service_subtypes_attributes][0][id]" type="hidden" value="2" /&gt;
     &lt;input id="service_request_services_attributes_0_service_subtypes_attributes_0_id" name="service_request[services_attributes][0][service_subtypes_attributes][0][id]" type="hidden" value="2" /&gt;
    &lt;input id="service_request_services_attributes_0_service_subtypes_attributes_1_id" name="service_request[services_attributes][0][service_subtypes_attributes][1][id]" type="hidden" value="4" /&gt;
     &lt;input id="service_request_services_attributes_0_service_subtypes_attributes_1_id" name="service_request[services_attributes][0][service_subtypes_attributes][1][id]" type="hidden" value="4" /&gt;
  </code></pre>
  
  <p>The submitted parameters look like this:</p>
  
  <pre><code>{
  ...other data...
   "services_attributes"=&gt; {
    "0"=&gt; {
     "types_uid_fk"=&gt;"1", 
     "service_subtypes_attributes"=&gt; {
      "0"=&gt;{"id"=&gt;"1"}, 
      "1"=&gt;{"id"=&gt;"2"}, 
      "2"=&gt;{"id"=&gt;"3"}
     }, 
     "cats_uid_fk"=&gt;"1"
    }
   }
  }
  </code></pre>
  
  <p>I get back "undefined method 'service_subtype' for #" error and the only table not updated is the join table between the HABTM models. Any idea how to solve this or what is happening behind the scenes? I'm not sure I understand the "magic" happening behind this procedure to see it working. It seems like most say that HABTM doesnt work with nested attributes. Seems to be the case. Work arounds?</p>