title: Ruby/Rails/Rspec - ActiveRecord::AssociationTypeMismatch:
body: <p>OK, here's my rspec code ...</p>
  
  <pre><code>        before(:each) do
              @attr = { :greeting =&gt; "Lorem ipsum", :recipient =&gt; @recipient }
          end
  
          it "should redirect to the home page" do
              puts "spec: @attr = #{@attr}"
              puts "spec: recipient = #{@attr[:recipient]}"
              post :create, :card =&gt; @attr
              response.should redirect_to(root_path)
          end
  </code></pre>
  
  <p>Now the output from this is:</p>
  
  <pre><code>spec: @attr = {:greeting=&gt;"Lorem ipsum", :recipient=&gt;#&lt;User id: 2, first_name: "Example", last_name: "User", email: "recipient@example.com", created_at: "2011-12-22 04:01:02", updated_at: "2011-12-22 04:01:02", encrypted_password: "2d1323ad5eb21fb5ae5e87dfa78a63b521c56833189cc049ee2...", salt: "2679fcc29a30e939541cb98cb65d1d508035fea0eff1136037a...", admin: false&gt;}
  spec: recipient = #&lt;User:0xac5d80c&gt;
  </code></pre>
  
  <p>So we can see that recipient is a User.</p>
  
  <p>On the controller side, we see have ... </p>
  
  <pre><code>def create
      puts "create: Params = #{params}"
      @card = current_user.sent_cards.build(params[:card])
      if @card.save
          flash[:success] = "Card created!"
          redirect_to root_path
      else
          render 'pages/home'
      end
  end
  </code></pre>
  
  <p>With a display of ...</p>
  
  <pre><code>create: Params = {"card"=&gt;{"greeting"=&gt;"Lorem ipsum", "recipient"=&gt;"2"}, "controller"=&gt;"cards", "action"=&gt;"create"}
  </code></pre>
  
  <p>and I see an error of ...</p>
  
  <pre><code>1) CardsController POST 'create' success should create a card
     Failure/Error: post :create, :card =&gt; @attr
     ActiveRecord::AssociationTypeMismatch:
       User(#90303150) expected, got String(#76171330)
     # ./app/controllers/cards_controller.rb:7:in `create'
     # ./spec/controllers/cards_controller_spec.rb:47:in `block (5 levels) in &lt;top (required)&gt;'
     # ./spec/controllers/cards_controller_spec.rb:44:in `block (4 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>So ... how did the User object get changed into its id as a string? Any ideas?</p>