Search code examples
ruby-on-railsmodelsforeign-key-relationship

accepts_nested_attributes_for not generating foreign key


I have a model

class ServiceRequest < ActiveRecord::Base
  has_many :services
  accepts_nested_attributes_for :services

  ...
end  

and the child

class Service < ActiveRecord::Base
  belongs_to :service_category, :foreign_key => "wsi_web_serv_cats_uid_fk"
  belongs_to :service_type, :foreign_key => "wsi_web_serv_types_uid_fk"
  belongs_to :service_subtype, :foreign_key => "wsi_web_srv_subtypes_uid_fk"
  belongs_to :service_requests, :foreign_key => "wsi_web_inq_audits_uid_fk"
end

and am creating it through a form like so:

<% form_for(@request, :url => { :action => :create }) do |form| %>
    <table>   
        <tr>
          <td><font style="font-weight: bold" color="red">*</font><b>First Name</b></td>
            <td><b>Middle Initial</b></td>
            <td><font style="font-weight: bold" color="red">*</font><b>Last Name</b></td>
        </tr>
        <tr>
            <td><%= form.text_field :first_name %></td>
            <td><%= form.text_field :win_middle_init, :size => "2" %></td>
            <td><%= form.text_field :last_name %></td>
        </tr>
        <tr>
            <td colspan="2">
                <font style="font-weight: bold" color="red">*</font><b>Address 1</b><br/>
                <%= form.text_field :address_1 %><br/>
                <%= form.text_field :address_2 %>
            </td>
        </tr>
        <tr>
            <td><font style="font-weight: bold" color="red">*</font><b>City</b></td>
            <td><font style="font-weight: bold" color="red">*</font><b>State</b></td>
            <td><font style="font-weight: bold" color="red">*</font><b>Zip Code</b></td>
        </tr>
        <tr>
            <td><%= form.text_field :municipality %></td>
            <td><%= form.text_field :state, :size => "4" %></td>
            <td><%= form.text_field :zip, :size => "9" %></td>
        </tr>
        <tr>
            <td>
                <font style="font-weight: bold" color="red">*</font><b>Phone Number</b><br/>
                <%= form.text_field :day_phone %>
            </td>
        <tr/>
        <tr>
            <td>
                <font style="font-weight: bold" color="red">*</font><b>Email</b><br/>
                <%= form.text_field :email %>   
            </td>
        <tr/>
        <tr>            
            <td>
                <font style="font-weight: bold" color="red">*</font><b>Confirm Email</b><br/>
                <%= form.text_field :email_confirmation %>
            </td>
        </tr>
        <tr>
            <td>
              <font style="font-weight: bold" color="red">*</font><b>Preferred Contact</b><br/>
              <%= form.select "contact_method", options_for_select([["Phone", "PHN"], ["Email", "EML"], ["Phone or Email", "BTH"]]) %>
            </td>
        </tr>
    </table>

    <% form.fields_for :services do |fields| %>
        <%= fields.hidden_field :wsi_web_serv_cats_uid_fk %>
        <%= fields.hidden_field :wsi_web_serv_types_uid_fk %>
        <%= fields.hidden_field :wsi_web_srv_subtypes_uid_fk %>
    <% end %>           

    <p>
        <%= form.submit "Create" %>
    </p>            
<% end %>   

The hidden fields are populated from a form on a page that this one is directed to from. This all works fine. My create method is as follows:

def create
    service_request = ServiceRequest.new(params[:service_request])

    if service_request.save!
        flash[:notice] = "Information submitted successfully. You will be contacted by a customer service representative regarding the services you selected."
        redirect_to :controller => "customer", :action => "index"
    else
        flash[:notice] = "Error submitting info. Please try again."
        redirect_to :back
    end         
end

Everything gets created in the database just fine. However, the two models are not linked by the foreign key. In otherwords, the foreign key is never set in the child model. How do I remedy this? I have seen some people say this is a documented bug, but I have been unable to find this to be true anywhere. Thanks for any help.


Solution

  • You need to provide the :foreign_key => "wsi_web_inq_audits_uid_fk" on the has_many in the ServiceRequest class as well. See the API Docs for all the available has_many parameters.