title: Find Most Recent Rails Record In Past
body: <p>I'm trying to create a Rails controller query that finds the record that is the most recent date in a column that is not in the future (which is why I can't do a simple <code>order</code> statement).  I found <a href="https://stackoverflow.com/questions/7978018/active-record-find-records-which-were-created-at-before-today">this post</a>, but the answer there (<code>MyModel.where("created_at &lt; ?", 2.days.ago)</code>) throws up some errors.</p>
  
  <p>I have this working properly, but if there's a week that starts in the future it selects that, not the most recent one in the past:</p>
  
  <pre><code>@most_recent = Plan.order("week_starting").last
  </code></pre>
  
  <p>Any thoughts as to how to do this properly in Rails?</p>
  
  <p>Here's my entire <code>plans#index</code> method:</p>
  
  <pre><code> def index
      @plans = Plan.where(user_id: current_user.id)
      @plan = Plan.new
      @recent = Plan.order("week_starting").last  &lt;&lt;&lt;THIS LINE
      @recipes = Recipe.where(user_id: current_user.id)
      @monday = Recipe.where(id: @recent.monday)[0]
      @tuesday = Recipe.where(id: @recent.tuesday)[0]
      @wednesday = Recipe.where(id: @recent.wednesday)[0]
      @thursday = Recipe.where(id: @recent.thursday)[0]
      @friday = Recipe.where(id: @recent.friday)[0]
      @saturday = Recipe.where(id: @recent.saturday)[0]
      @sunday = Recipe.where(id: @recent.sunday)[0]
    end
  </code></pre>
  
  <p>Basically, I need the <code>plan</code> for the current user which has the <code>week_starting</code> value that is closest to <code>Date.today</code> yet still in the past.</p>