title: Get controller#index with joins to pass with rspec
body: <p>I have a Lead model</p>
  
  <pre class="lang-rb prettyprint-override"><code># A Lead is a type of User that might be interested in using our service.
  class Lead &lt; User
    validates :first_name, presence: true
    has_many :notes, dependent: :destroy
  
    def self.search(search)
      ...
    end
  end
  </code></pre>
  
  <p>A Lead inherits from a User</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_one :location, dependent: :destroy
    accepts_nested_attributes_for :location
  
    delegate :address, :city, :state, :zip_code,
             :full_location, :latitude, :longitude,
             to: :location, allow_nil: true
  end
  </code></pre>
  
  <p>All Users have one <code>Location</code>, which includes data like <code>:address, :city, :state</code>, etc.</p>
  
  <p>I have this controller</p>
  
  <pre class="lang-rb prettyprint-override"><code>class LeadsController &lt; ApplicationController
    helper_method :sort_column
  
    def index
      @leads = Lead.search(params[:search])
                   .joins(:location)
                   .order("#{sort_column} #{sort_direction}")
                   .page(params[:page])
                   .per(8)
    end
  end
  </code></pre>
  
  <p>I have this test</p>
  
  <pre class="lang-rb prettyprint-override"><code>describe LeadsController, type: :controller do
    before do
      @lead = create :lead
    end
  
    describe 'GET #index' do
      it 'populates an array of leads' do
        get :index
        assigns(:leads).should eq [@lead]
      end
  end
  </code></pre>
  
  <p>The spec <em>fails</em> and says <code>@leads</code> is empty.</p>
  
  <p>The spec <em>passes</em> when I delete the line <code>.joins(:location)</code> in the <code>LeadsController</code></p>
  
  <p>Everything works fine in <code>development</code>. The app is able to pull up all the correct data and display it.</p>
  
  <p>For some reason, <code>.joins</code> in the <code>test</code> environment causes <code>@leads</code> to be empty.</p>
  
  <p>I need that line. I need it to be able to sort Leads(Users) by their zip_code. Remember, zip_code is stored in a <code>Location</code> object.</p>
  
  <p>My question is: How do I get my spec passing while keeping sortability for zip codes? What's happening differently in the <code>test</code> environment?</p>
  
  <p>Thanks!</p>