title: Rails Includes Error : UndefinedTable: ERROR: missing FROM-clause entry for table
body: <p>I prefer use includes to connect my three tables. I have three model that I need to connect such as Register, Student, and Schedule. Here is my model Association</p>
  <pre><code>class Student &lt; ApplicationRecord
      belongs_to :register
  end
  
  class Register &lt; ApplicationRecord
      has_one :student
  
      belongs_to :schedule
  end
  
  class Schedule &lt; ApplicationRecord
      belongs_to :course
      belongs_to :teacher
  
      has_many :days, :dependent =&gt; :destroy, :class_name =&gt; 'ScheduleDay'
      has_many :registers
  end
  </code></pre>
  <p>Here is my controller</p>
  <pre><code>def index
          @students = Student.includes([register: :schedule])
          @students = @students.order 'students.created_at DESC'
  
  
          if params[:status_id] &amp;&amp; params[:status_id].to_i &gt; 0
              @students = @students.where 'students.status_id = ?', params[:status_id]
          end
  
          if params[:id] &amp;&amp; params[:id].to_i &gt; 0
              @students = @students.where 'cast(students.id as varchar) like (?)', &quot;%#{params[:id]}%&quot;
          end
  
          if params[:full_name] &amp;&amp; params[:full_name].to_s.length &gt; 0
              @students = @students.where 'lower(registers.name_in_indonesian || registers.name_in_chinese) like lower(?)', &quot;%#{params[:full_name]}%&quot;
          end
  
          if params[:course_id] &amp;&amp; params[:course_id].to_i &gt; 0
              @students = @students.where 'schedules.course_id = ?', params[:course_id]
          end
  
          @students = @students.paginate(page: params[:page], per_page: 30)
  end
  </code></pre>
  <p>I got following error :</p>
  <pre><code>PG::UndefinedTable: ERROR:  missing FROM-clause entry for table &quot;schedules&quot;
  LINE 1: SELECT &quot;students&quot;.* FROM &quot;students&quot; WHERE (schedules.course_...
  </code></pre>
  <p>I tried to debug by using rails console but end up with <strong>(Object doesn't support #inspect)</strong>
  <a href="https://i.stack.imgur.com/p5Oqe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p5Oqe.png" alt="enter image description here" /></a></p>