title: Created nested model setting a property on nested model before save
body: <p>I have two models a <code>Company</code> and a <code>User</code> the Company <code>has_many :users</code> and the User <code>belongs_to :company</code>.  I have a form such as:</p>
  
  <p><code>&lt;%= form_for @company, data: {toggle: :validator}, novalidate: "novalidate", html: {role: :form} do |f| %&gt;</code></p>
  
  <p><strong>company fields</strong></p>
  
  <p>Then in there I have</p>
  
  <p><code>&lt;%= f.fields_for :users, @company.users.build do |user_form| %&gt;</code></p>
  
  <p><strong>A bunch of user fields</strong></p>
  
  <p>It posts the data with the nested attributes of <code>users_attributes: {"0" =&gt; {name: "Chad"}}</code></p>
  
  <p>But it doesn't create the user only the company object.</p>
  
  <p><strong>Company Model</strong></p>
  
  <pre><code>class Company &lt; ActiveRecord::Base
    has_many :users, dependent: :destroy
    has_many :contacts, dependent: :destroy
  
    accepts_nested_attributes_for :users
    accepts_nested_attributes_for :contacts
  
    attr_accessor :card_token, :users_attributes
  
    before_create :create_company_customer_token
    before_create :create_admin_user
    before_destroy :set_deleted_flag
  
    validates_presence_of :name, :phone_number
  
    private
  
    def create_admin_user
      self.users.first.admin = true
    end
  
    def set_deleted_flag
      self.deleted = true
      save
  
      users.each do |u|
        u.destroy
      end
  
      false
    end
  
    def create_company_customer_token
      begin
        customer = Stripe::Customer.create(description: "Company: #{self.name}", card: self.card_token, plan: self.plan)
        self.stripe_customer_id = customer['id']
      rescue Stripe::StripeError =&gt; e
        self.errors.add(:stripe_customer_id, "Looks like we are having an issue at the moment, please try again shortly")
        @logger ||= Rails.logger
        @logger.error(e)
      end
    end
  
  end
  </code></pre>
  
  <p><strong>User Model</strong></p>
  
  <pre><code>class User &lt; ActiveRecord::Base
  include Clearance::User
  
    has_many :messages
    belongs_to :company
  
    before_destroy :set_deleted_flag
    after_create :send_welcome_email
  
    validates_presence_of :first_name, :last_name
    validates_uniqueness_of :email, scope: :company_id, conditions: -&gt; { where.not(deleted: true) }
  
    def name
      "#{first_name} #{last_name}"
    end
  
    private
  
    def set_deleted_flag
      self.deleted = true
      save
    end
  
    def send_welcome_email
      UserMailer.welcome_email(self).deliver
    end
  
  end
  </code></pre>