Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I unable to save an associated object in a has_one relationship?
    primarykey
    data
    text
    <p>I've got a pickem object that has one result. I'm having an issue getting the result to save properly to the database.</p> <p>pickems_controller.rb</p> <pre><code>def results @pickem = Pickem.find params[:id] # @pickem.result = @pickem.build_result if @pickem.result.blank? @pickem.result = Result.new end def update_results @pickem = Pickem.find params[:id] @pickem.result = Result.new params[:pickem][:result_attributes] if @pickem.result.update_attributes params[:pickem][:result_attributes] redirect_to edit_pickem_results_path(@pickem), :notice =&gt; 'The results have been successfully updated.' else render "edit" end end </code></pre> <p>results.html.erb</p> <pre><code>&lt;%= simple_form_for @pickem, :url =&gt; edit_pickem_results_path(@pickem), :method =&gt; :put, do |f| %&gt; &lt;%= f.simple_fields_for :result do |r| %&gt; &lt;%= r.input :first_name %&gt; ... &lt;% end %&gt; &lt;%= f.submit :class =&gt; 'btn btn-success', :label =&gt; 'Submit Results' %&gt; &lt;% end %&gt; </code></pre> <p>pickem.rb</p> <pre><code>has_one :result, :dependent =&gt; :destroy accepts_nested_attributes_for :result </code></pre> <p>result.rb</p> <pre><code>belongs_to :pickem </code></pre> <p>I was initially using the build_result code that is commented out in the controller but had to back out of that. With build_result a result record was saved to the database the instant somebody clicked into the results form. There are rules in place in the application that don't allow users to make any picks if a result has been entered. So even if a user clicked into the result form but didn't submit, the result was still being created.</p> <p>How can I build my form and save the result record only when the user clicks save, and not when the form is loaded? The current solution I've pasted above does not work. It saves a result record with the appropriate foreign key but never gets the form data. If I dump @pickem.result the correct form data is in the result object, I just can't get it to save right. Other solutions I've tried save the form data correctly but have a foreign key of 0.</p> <p>EDIT: For whatever reason <code>@pickem.result = Result.new</code> was still saving a record to the database so I changed it to <code>@result = Result.new</code> and updated the form as follows:</p> <pre><code>&lt;%= simple_form_for @result, :url =&gt; edit_pickem_results_path(@pickem), :method =&gt; :put, do |r| %&gt; &lt;%= r.input :first_name %&gt; &lt;%= r.submit :class =&gt; 'btn btn-success', :label =&gt; 'Submit Results' %&gt; &lt;% end %&gt; </code></pre> <p>Then using the suggestion from Chuck W of <code>@result = @pickem.result.build params[:result]</code>, I get <code>undefined method</code>build' for nil:NilClass`.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload