title: ActiveRecord Unambiguous Query with Joining table
body: <p>I am trying to combine two common active record queries.</p>
  
  <ul>
  <li>I need to perform a where clause on a date field</li>
  <li>I need to join 2 tables</li>
  </ul>
  
  <p>Since I am joining them I need to specify the table of the column, a column both tables have ,<code>created_at</code>.</p>
  
  <p>I basically need to combine these 2 answers:</p>
  
  <p><a href="https://stackoverflow.com/questions/50111362/activerecord-joins-throwing-column-in-where-clause-is-ambiguous-error-rails-5">ActiveRecord joins throwing Column in where clause is ambiguous error - Rails 5.1</a></p>
  
  <pre class="lang-rb prettyprint-override"><code>    @projects = @projects.joins(:categories).where(categories: { id: params[:category_id] }) if params[:category_id].present?
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/7978018/active-record-find-records-which-were-created-at-before-today">Active Record - Find records which were created_at before today</a></p>
  
  <pre class="lang-rb prettyprint-override"><code>MyModel.where("created_at &lt; ?", 2.days.ago)
  
  </code></pre>
  
  <p>What I have right now is this:</p>
  
  <pre class="lang-rb prettyprint-override"><code>Train.joins(:schedule).where('created_at &gt;= ?', DateTime.now - 7.days)
  </code></pre>
  
  <p>Which returns:</p>
  
  <pre><code>Mysql2::Error: Column 'created_at' in where clause is ambiguous: SELECT 
  </code></pre>