English 中文(简体)
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.

最佳回答

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.

问题回答

Why is this plural?

class Service < ActiveRecord::Base
  ...
  belongs_to :service_requests, :foreign_key => "wsi_web_inq_audits_uid_fk"
end




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签