1

我有一个模型

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

  ...
end  

和孩子

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

并通过如下形式创建它:

<% 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 %>   

隐藏字段是从该页面指向的页面上的表单填充的。这一切都很好。我的创建方法如下:

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

一切都在数据库中创建得很好。但是,这两个模型没有通过外键链接。换句话说,外键永远不会在子模型中设置。我该如何补救?我看到有人说这是一个记录在案的错误,但我无法在任何地方找到这是真的。谢谢你的帮助。

4

2 回答 2

2

您还需要:foreign_key => "wsi_web_inq_audits_uid_fk"在 ServiceRequest 类中提供 has_many 。有关所有可用的 has_many 参数,请参阅API 文档

于 2009-11-20T22:17:06.943 回答
1

为什么是复数?

class Service < ActiveRecord::Base
  ...
  belongs_to :service_requests, :foreign_key => "wsi_web_inq_audits_uid_fk"
end
于 2009-11-20T22:31:17.010 回答