title: Rails 4: How to use includes() with where() to retrieve associated objects
body: <p>I can't figure out how to user the <code>.where()</code> method to retrieve associated model data. In this example, Projects belongs_to Users...</p>
  
  <pre><code>class Project &lt; ActiveRecord::Base
      belongs_to :user
      has_many :videos
  end
  
  class User &lt; ActiveRecord::Base
      has_many :projects
  end
  
  class ProjectsController &lt; ApplicationController
    def invite
      @project = Project.includes([:user]).where( {:hashed_id=&gt;params[:id]} ).first
    end
  end
  </code></pre>
  
  <p>In App/views/projects/invite.html.erg <code>&lt;%= debug( @project ) %&gt;</code> returns:</p>
  
  <pre><code>--- !ruby/object:Project
  attributes:
    id: 22
    name: Some Project Name
    belongs_to: 1
    instructions: Bla bla bla
    active: true
    max_duration: 2
    max_videos: 
    created_at: 2013-08-26 15:56:50.000000000 Z
    updated_at: 2013-08-26 15:56:50.000000000 Z
    hashed_id: '1377532589'
  </code></pre>
  
  <p>Shouldn't the associated User hash/array be included in this? I know I could manually add it by calling a second <code>find</code>/<code>where</code> ( <code>@project.user = User.where( {:id=&gt;@project.belongs_to}</code> ) but this doesn't feel like "The Rails Way". What is?</p>
  
  <p><strong>Solution</strong>
  My initial question was formulated under the incorrect assumption that <code>debug()</code> would return associated objects (this works in cakePHP because it bundles everything into arrays).   </p>
  
  <p>So my original code <em>should</em> work. However, I had incorrectly named the foreign key filed in the table. I got confused by looking at the migration method <code>t.belongs_to</code> (which automatically creates the correctly named foreign_key field, <em>not</em> a field named "belongs_to"). So I also had to rename that column to <code>user_id</code> and now it works just as described in @Veraticus's answer below.</p>