title: ActiveRecord::RecordNotFound: Couldn't find Debtor with id=
body: <p>I recently converted a project from MongoDB/mongoid to PostgreSQL.  After the transition, all of my model specs are passing and I have most controller specs passing.  Three of my controllers are not passing tests though.</p>
  
  <p>The Alert, Order, and User models are dependent on the Client and Debtor Models per the below model definitions:</p>
  
  <pre><code>class Client &lt; ActiveRecord::Base
    has_many :users, autosave: true, dependent: :destroy
    has_many :alerts, autosave: true, dependent: :destroy
    has_many :orders, autosave: true, dependent: :destroy
  
    ...
  end
  
  class Debtor &lt; ActiveRecord::Base
    has_many :orders, autosave: true
    has_many :alerts, autosave: true
  
    ...
  end
  
  class Alert &lt; ActiveRecord::Base
    belongs_to :client
    belongs_to :debtor
  
    ...
  end
  
  class Order &lt; ActiveRecord::Base
    belongs_to :client
    belongs_to :user
    belongs_to :debtor
  
    ...
  end
  
  class User &lt; ActiveRecord::Base
    rolify
  
    devise :database_authenticatable, 
       :recoverable, :rememberable, :trackable, :validatable
  
    has_and_belongs_to_many :roles, :join_table =&gt; :users_roles
    belongs_to :client
    has_many :orders
  end
  </code></pre>
  
  <p>Almost all of the failed tests have the same error (some have "Couldn't find Client with id= "):</p>
  
  <pre><code> 129) OrdersController logged in as a regular user GET #new renders the new template
   Failure/Error: get :new, client_id: @user.client_id
   ActiveRecord::RecordNotFound:
     Couldn't find Debtor with id=
   # ./spec/controllers/orders_controller_spec.rb:59:in `block (4 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>I have ran db:test:prepare and the drop, create, migrate sequence.  </p>
  
  <p>Update: Here's the related spec from orders_controller_spec.rb and the related controller.  I'm a rspec noob, so I'll apologize now if the <code>before(:each)</code> block is a crime against humanity.</p>
  
  <pre><code>require 'spec_helper'
  
  describe OrdersController do
    before (:each) do
      @user, @user2, @user3, @admin = FactoryGirl.create(:user), FactoryGirl.create(:user), FactoryGirl.create(:user), FactoryGirl.create(:user)
      @client, @client2 = FactoryGirl.create(:client), FactoryGirl.create(:client)
      @debtor, @debtor2, @debtor3 = FactoryGirl.create(:debtor), FactoryGirl.create(:debtor), FactoryGirl.create(:debtor)
      @order, @order2, @order3, @order4 = FactoryGirl.create(:order), FactoryGirl.create(:order), FactoryGirl.create(:order),FactoryGirl.create(:order)
      @role = FactoryGirl.create(:role)
      @admin_role = FactoryGirl.create(:admin_role)
      @vip_role = FactoryGirl.create(:vip_role)
      @user.roles &lt;&lt; @role
      @user2.roles &lt;&lt; @vip_role
      @user3.roles &lt;&lt; @role
      @admin.roles &lt;&lt; @admin_role
      @client.users &lt;&lt; @user &lt;&lt; @admin
      @client.orders &lt;&lt; @order &lt;&lt; @order2
      @client2.users &lt;&lt; @user2 &lt;&lt; @user3
      @client2.orders &lt;&lt; @order3 &lt;&lt; @order4
    end
  
    context "logged in as a regular user" do
      before { sign_in @user }
  
      describe "GET #index" do
        before { get :index, client_id: @user.client_id }
  
        it "assigns a list of orders belonging to user client" do
          assigns(:orders).to_a.should eq([@order, @order2])
        end
        it "renders the :index template" do
          response.should render_template :index, client_id: @user.client_id
        end
      end
  
      describe "GET #show" do
        context "for order belonging to user client" do
          before { get :show, client_id: @user.client_id, id: @order }
  
          it "assigns the requested order to @order" do
            assigns(:order).should eq(@order)
          end
          it "renders the :show template" do
            response.should render_template :show, client_id: @user.client_id, id: @order
          end
        end
  
        context "for order not belonging to user client" do
          before { get :show, client_id: @client2.id, id: @order3 }
  
          it "redirects to the home view" do
            response.should redirect_to root_url
          end
        end 
      end
  
      describe "GET #new" do
        it "renders the new template" do
          get :new, client_id: @user.client_id
          response.should render_template :new, client_id: @user.client_id
        end
      end
  
      ...
    end
  end
  </code></pre>
  
  <p>The related controller actions:</p>
  
  <pre><code>class OrdersController &lt; ApplicationController
    before_filter :authenticate_user!
    before_filter :load_client
    before_filter :has_enough_units, :only =&gt; [:create]
    respond_to :html
    load_and_authorize_resource :debtor
    load_and_authorize_resource :order, :through =&gt; :client, :shallow =&gt; true
  
    def index
      if @client == nil
        @orders = Order.all
      else
        @orders = @client.orders.all
      end
      respond_with(@orders)
    end
  
    def show
      if @client == nil
        @order = Order.find(params[:id])
      else
        @order = @client.orders.find(params[:id])
      end
    end
  
    def new
      if current_user.has_role? :admin
        @order = Order.new
      else
        @order = @client.orders.build
      end
      @order.build_debtor
      @clients = Client.all.to_a
    end
  
    ...
  end
  </code></pre>
  
  <p>Hopefully this is enough information...</p>