title: RoR - The record failed to save when after its foreign key was set to nil
body: <p>Problem:</p>
  
  <pre><code>LocationsController request to edit should load the requested location
  Failure/Error: @location.image = @image
       ActiveRecord::RecordNotSaved:
         Failed to remove the existing associated image. 
  The record failed to save when after its foreign key was set to nil.
  
  LocationsController request to edit should be successfull
  Failure/Error: @location.image = @image
       ActiveRecord::RecordNotSaved:
         Failed to remove the existing associated image. 
  The record failed to save when after its foreign key was set to nil.
  </code></pre>
  
  <p>Part of the Controller:</p>
  
  <pre><code>def new
   @location = Location.new
  end
  
  def create
    @location = Location.new(params[:location])
    @location.owner_id = current_user.id
    @address = Address.new(params[:address])
    @location.image = Image.new(params[:image])
    responds_to_parent do
      if @location.save &amp;&amp; @location.image.update_from_uploaded_data(params[:image])
        @address.addressable = @location
        @address.save
        @locations = Location.all_as_select_options
        respond_to do |format|
          format.js { render layout: false }
        end
      else
        render :update
      end
    end
  end
  
  def edit
    @address = @location.address
  end
  
  def update
    @location.image ||= Image.new
    @location.address ||= Address.new
    address = params[:address]
  
    if @location.update_attributes(params[:location]) &amp;&amp;   @location.address.update_attributes(address) &amp;&amp; @location.image.update_from_uploaded_data(params[:image])
      flash[:notice] = 'Daten für den Ort wurden geändert.'
      redirect_to location_path(@location)
    else
      flash[:notice] = 'Die Daten konnten nicht gespeichert werden'
      render action: 'edit'
    end
  end
  
  def destroy
    @location.destroy if @location.owner.id == current_user.id
    redirect_to action: 'index', controller: 'locations'
  end
  
  private
  
  def load_location
    @location = Location.find(params[:id])
  end
  </code></pre>
  
  <p>Rspec:</p>
  
  <pre><code>describe 'request to edit' do
        before do
          login(mock_admin)
          @location = create_location
          @image = create_image
          Location.stub!(:find).and_return(@location)
          Image.stub!(:find).and_return(@image)
          @location.image = @image
          get :edit, id: @location.id
        end
        it 'should be successfull' do
          response.should be_success
        end
        it 'should load the requested location' do
          assigns(:location).should eql(@location)
        end
        it 'should load the locations address' do
          assigns(:address).should eql(@location.address)
        end
      end
  </code></pre>
  
  <p>My attempt: Allowing parameters like location and image in my edit method. Like it was suggested at <a href="https://stackoverflow.com/questions/18984093/cant-update-my-nested-model-form-for-has-one-association">Can&#39;t update my nested model form for has_one association</a></p>
  
  <p>I tried the </p>
  
  <pre><code>'params.require(:...).permit(...)'
  </code></pre>
  
  <p>and</p>
  
  <pre><code>accepts_nested_attributes_for(:..., update_only: true)
  </code></pre>
  
  <p>solution which were suggested. But even with all parameters allowing it didn't work out. Do you know why though? I tried to modify the methods but o well...</p>
  
  <p>Edit -- -- Model:</p>
  
  <pre><code>class Location &lt; ApplicationRecord
    belongs_to  :owner, class_name: '::User', foreign_key: 'owner_id'
    has_one     :address, as: :addressable, :dependent =&gt; :destroy
    has_one     :image, as: :visualisable
    has_many    :events
  
    validates_presence_of :owner_id, :name
  
    def display_name
      [name.to_s, address&amp;.display].compact.join(', ')
    end
  
    def self.all_as_select_options
      all.collect { |m| [m.name, m.id] }
    end
  
    def can_be_deleted_by?(user)
      owner == user &amp;&amp; events.count == 0
    end
  end
  </code></pre>