Сохранить account_id в таблице вложенных людей - PullRequest
0 голосов
/ 10 декабря 2011

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

In account_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 в таблице people равен nil. Как я могу это исправить?

...