title: How to scope last week by Date object
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/8505997/scoping-date-attribute-for-this-week">Scoping date attribute for this week?</a>  </p>
  </blockquote>
  
  
  
  <p>I am trying to scope all of my Products for this week, so it should show all the products leading up to whichever day of the week.</p>
  
  <pre><code>class Product &lt; ActiveRecord::Base
     attr_accessible :purchase_date
  
     def self.last_week # All prices of last week.
        where(:purchase_date =&gt; 1.week.ago)
     end
  
     create_table :products do |t|
        t.date :purchase_date
     end
  end
  </code></pre>
  
  <p>This code renders nothing in the view though so what do I need to correct?</p>
  
  <hr>
  
  <p><strong>ANSWER</strong></p>
  
  <p>For some reason I had to add <code>advance(:days =&gt; -1)</code> to in order to also retrieve Monday as well. You may not have to do this though.</p>
  
  <pre><code>def self.last_week
      where(:purchase_date =&gt; 1.week.ago.beginning_of_week.advance(:days =&gt; -1)..1.week.ago.end_of_week).order("purchase_date desc")
  end
  </code></pre>
  
  <p><strong>UPDATED ANSWER</strong></p>
  
  <p>I had to do the <code>advance(:days =&gt; -1)</code> because of the Time zone I am in. I got rid of this by making sure I'm in my own Time zone. So now it can be normal as it should be: </p>
  
  <pre><code>def self.last_week
    where(:purchase_date =&gt; 1.week.ago.beginning_of_week..1.week.ago.end_of_week)
  end
  </code></pre>
  
  <p>And it should work correctly ONLY if you go by the default Rails Time zone or you config your own:</p>
  
  <p>app/config/environment/development.rb</p>
  
  <pre><code>config.time_zone = "Eastern Time (US &amp; Canada)"
  </code></pre>
  
  <p>Good luck.</p>