title: Rails3 Nested Attributes Using Parent Values in Save Action
body: <p>I've got a simple nested form working in my rails3 application. I'm trying to work out how to save a value from the parent model into the child when saving.</p>
  
  <pre><code>class Radcheck &lt; ActiveRecord::Base
    set_table_name 'radcheck'
    attr_accessible :attribute_name, :username, :value, :op
    belongs_to :raduser  
  end
  
  class Raduser &lt; ActiveRecord::Base
    has_many :radcheck, :dependent =&gt; :destroy
    accepts_nested_attributes_for :radcheck  
  end
  </code></pre>
  
  <p>And my form:</p>
  
  <pre><code>&lt;%= form_for @raduser do |f| %&gt;  
    &lt;p&gt;  
      &lt;%= f.label :username %&gt;&lt;br /&gt;  
      &lt;%= f.text_field :username %&gt;  
    &lt;/p&gt;  
    &lt;%= f.fields_for :radcheck do |builder| %&gt;  
    &lt;li&gt;  
      &lt;%= builder.label :attribute_name %&gt;&lt;%= builder.text_field :attribute_name %&gt;  
  
    &lt;/li&gt;  
    &lt;% end %&gt;  
    &lt;p&gt;&lt;%= f.submit "Submit" %&gt;&lt;/p&gt;  
  &lt;% end %&gt;
  </code></pre>
  
  <p>What I want to do is save the Raduser.username value in to the radcheck table on save. For each record.</p>
  
  <p>I've tried putting something in the controller but that wasn't really working for us.</p>
  
  <p>-- Update --</p>
  
  <p>In my radcheck controller, I've tried this (as a test) but the values don't save.</p>
  
  <pre><code>def create
        @radcheck = Radcheck.new(params[:radcheck])
        @radcheck.username = '123'
        respond_to do |format|
          if @radcheck.save
  
            format.html { redirect_to @radcheck, notice: 'Radcheck was successfully created.' }
            format.json { render json: @radcheck, status: :created, radcheck: @radcheck }
          else
            format.html { render action: "new" }
            format.json { render json: @radcheck.errors, status: :unprocessable_entity }
          end
        end
      end
  </code></pre>
  
  <p>Have also tried in radusers but that gave error.</p>