title: PG::ObjectNotInPrerequisiteState: ERROR: cannot insert into view
body: <p>I am extremely confused about an error that I am receiving for one of my controller actions. The error that I receive for one action <code>positions#one_click_create</code> yields me this error:</p>
  
  <pre><code>ActiveRecord::StatementInvalid: PG::ObjectNotInPrerequisiteState: ERROR:  cannot insert into view "contributions" DETAIL:  Views that do not select from a single table or view are not automatically updatable.
  
  
  HINT:  To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule.
      : INSERT INTO "contributions" ("person_id", "project_id", "position_id", "name_fact_id") VALUES ($1, $2, $3, $4) RETURNING "id"
          test/controllers/positions_controller_test.rb:420:in `block (3 levels) in &lt;top (required)&gt;
  </code></pre>
  
  <p>The error goes on to suggest a postgresql trigger INSTEAD OF INSERT trigger.</p>
  
  <p>I've read the relevant link found here <a href="http://www.postgresql.org/docs/8.2/static/rules-update.html" rel="nofollow">INSTEAD OF INSERT</a> but I still don't understand what the problem is and even how to attempt to solve it. I have a simple get request, I don't understand why it should not work.</p>
  
  <p>For starters, I have this code in my view.</p>
  
  <pre><code>- options = { project_name: contribution.project.url_name,
            committer_name: contribution.contributor_fact.name.name,
            invite: params[:invite] }
  
  %button.btn.btn-primary
     = link_to t('.claim'), one_click_create_account_positions_path(options.merge(account_id: 'me')), { style: 'color: white' }
  </code></pre>
  
  <p>The code hits the get action for <code>positions#one_click_create</code> not before going through some before_actions, for finding relevant models.</p>
  
  <p>The one_click_create method is found below.</p>
  
  <pre><code> def one_click_create
    result = current_user.position_core.ensure_position_or_alias!(@project, @name)
  
    if result.class == Alias
      flash[:success] = t('.one_click_commit_claim', name: @name.name, preferred_name: result.preferred_name.name)
      redirect_to acount_positions_path(current_user)
    elsif result.class == Position
      flash[:success] = t('.one_click_contribution_success', name: @name.name)
      redirect_to accounts_positions_path(current_user)
    end
  
    flash[:success] = t('.one_click_submit_form', name: @name.name)
    redirect_to new_account_position_path(current_user,
                                        project_name: @project.name,
                                        committer_name: @name.name,
                                        invite: params[:invite])
  
   end
  </code></pre>
  
  <p>In my particular case the conditional logic is not important. What is important is that when it reaches the final line to redirect to the new_account_position_path, I receive the error as illustrated by this controller test.</p>
  
  <pre><code>it 'should successfully redirect to account new page with proper params' do
    position = create_position
    contribution = create(:contribution, position: position)
    get :one_click_create, account_id: 'me', params: { project_name: contribution.project.url_name,
                            committer_name: contribution.contributor_fact.name.name,
                            invite: params[:invite]
    must_redirect_to new_account_position_path(account_id: 'me')
  end
  </code></pre>
  
  <p>Can someone help to shed some light on what is the problem here and why it 'cannot insert into view :contributions"'?</p>