在嵌套人员表中保存account_id

时间:2011-12-09 22:35:51

标签: ruby-on-rails ruby forms associations

有3个型号

Account
has_many organizations, dependent destroy
has_many people, dependent destroy
accepts_nested_attributes_for organizations
accepts_nested_attributes_for people

Organization
belongs_to account
has_many people, dependent destroy
accepts_nested_attributes for people

People
belongs_to account
belongs_to organization

所以,在新的帐户表格中。我有类似的东西......

<%= form_for(@account) do |f| %>
  <%= f.fields_for :organizations do |builder| %>
    <%= render 'organizations_fields', :f => builder %>
  <% end %>

  <%= f.fields_for :people do |builder| %>
    <%= render 'people_fields', :f => builder %>
  <% end %>

  <div class="actions">
    <%= f.submit 'Crear cuenta' %>
  </div>
<% end %>

在accounts_controller.rb

  def new
    @account = Account.new
    organization = @account.organizations.build
    organization.people.build
    ...

  def create
    @account = Account.new(params[:account])

    respond_to do |format|
      if @account.save
      ....

所以一切正常......

  • account_id已保存到组织表
  • organization_id将保存到人员表

问题是,人员表中的account_id列是零。我该如何修复?

0 个答案:

没有答案