Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating object with belongs_to associations and nested_attributes
    primarykey
    data
    text
    <p>I've got problems with making update action for one of my data objects. I've got:</p> <pre><code>class UserProfile &lt; ActiveRecord::Base belongs_to :address, :dependent =&gt; :destroy belongs_to :post_address, :class_name =&gt; 'Address', :dependent =&gt; :destroy accepts_nested_attributes_for :address accepts_nested_attributes_for :post_address # validations and stuff end class Address &lt; ActiveRecord::Base # validations and stuff end </code></pre> <p>And the problem is with the form and action:</p> <pre><code>= form_for @up, :url =&gt; '/profile/edit', :method =&gt; :post do |f| = f.error_messages #... = f.fields_for :address, @up.address do |a| #... = f.fields_for :post_address, @up.post_address do |a| #... .field.push = f.submit 'Save', :class=&gt;'ok' </code></pre> <p>Action:</p> <pre><code>def edit_account @user = current_user if request.post? @up = @user.user_profile.update_attributes(params[:user_profile]) if @up.save redirect_to '/profile/data', :notice =&gt; 'Zmiana danych przebiegła pomyślnie.' end else @up = @user.user_profile end end </code></pre> <p>The error I get looks like this:</p> <pre><code>Couldn't find Address with ID=3 for UserProfile with ID=2 </code></pre> <p>And it occurs in the line: </p> <pre><code>@up = @user.user_profile.update_attributes(params[:user_profile]) </code></pre> <p>I think that AR tries to create another <code>Address</code> when the form is submitted but I'm not certain.</p> <p>Why do I get this error? What's wrong with my code?</p>
    singulars
    1. This table or related slice is empty.
    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