title: Creating model and nested model (1:n) at once with ActiveRecord
body: <p>My Rails5 application has an organization model and a user model (1:n relationship). The workflow of creating an organization should include the creation of the organization's first user as well. I thought this would be able with ActiveRecord through nested models, however the create action fails with the error message "Users organization must exist".</p>
  
  <pre><code>class Organization &lt; ApplicationRecord
      has_many :users, dependent: :destroy
      accepts_nested_attributes_for :users
  end
  
  class User &lt; ApplicationRecord
      belongs_to :organization
  end
  
  class OrganizationsController &lt; ApplicationController
      def new
          @organization = Organization.new
          @organization.users.build
      end
  
      def create
          @organization = Organization.new(organization_params)
          if @organization.save
              redirect_to @organization
          else
            render 'new'
          end
      end
  
      def organization_params
          params.require(:organization).permit(:name, users_attributes: [:name, :email, :password, :password_confirmation])
      end
  end
  </code></pre>
  
  <p>In the view I use the <code>&lt;%= f.fields_for :users do |user_form| %&gt;</code> helper.</p>
  
  <p>Is this a bug on my side, or isn't this supported by ActiveRecord at all? Couldn't find anything about it in the rails guides. After all, this should be (theoretically) possible: First do the INSERT for the organization, then the INSERT of the user (the order matters, to know the id of the organization for the foreign key of the user).</p>