# Question about a date range selector

**URL:** <https://www.ruby-forum.com/t/question-about-a-date-range-selector/171201>\
**Category:** Rails\
**Created:** [June 28, 2009, 2:02pm UTC](https://www.ruby-forum.com/t/question-about-a-date-range-selector/171201 "2009-06-28T14:02:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [June 28, 2009, 2:02pm UTC](https://www.ruby-forum.com/t/question-about-a-date-range-selector/171201/1 "2009-06-28T14:02:37Z")

</div>

All of my stat tables house data that is compiled weekly. I know how to  
add a date range selector to the table but I’m not sure how I can  
restrict the selector to show specific weeks only unless I use a  
symbolic type of selector.

For instance:

Suppose there are 13 weeks in a season. Data is added each of the 13  
weeks. A person wants to view just the data from week 1. In my table  
week 1 would have been between the date ranges of say Aug 31 - Sep 6.

Should I just use a symbolic selector instead with options for say:  
Week 1  
Week 2  
Week 3…

And then provide a case for when that week is selected it is sent over  
to a defined named\_scope for that particular week?

I’m just thinking about possibilities and what might be a better way of  
doing things.

Thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [June 28, 2009, 3:02pm UTC](https://www.ruby-forum.com/t/question-about-a-date-range-selector/171201/2 "2009-06-28T15:02:08Z")

</div>

I was playing around with distinct and tried the following:

\<%= collection\_select(:rushing\_offense, :compiled\_on,  
RushingOffense.find(:all, :select =\> ‘DISTINCT compiled\_on’), :id,  
:compiled\_on, {:prompt =\> true}) %\>

With my current week’s data for instance there are 120 timestamps.  
Using the above, it pulled 2 options only (which is not bad but also not  
good). The difference was within a second apart:

Please Select–  
2009-06-28 12:08:40 UTC  
2009-06-28 12:08:41 UTC

Probably because it took a second to load the batch. I would rather  
work with something like this but tailor it some more…

I’ll see what I can work out but if anyone has better ideas please let  
me know.

thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [June 29, 2009, 5:49pm UTC](https://www.ruby-forum.com/t/question-about-a-date-range-selector/171201/3 "2009-06-29T17:49:30Z")

</div>

One thing - if you’re only ever going to be interested in the date,  
I’d recommend changing the compiled\_on field to a date, rather than a  
datetime. That will eliminate the “off by 1 second” thing you’re  
seeing.

–Matt J.

On Jun 28, 9:02 am, “Älphä Blüë” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [June 30, 2009, 1:04am UTC](https://www.ruby-forum.com/t/question-about-a-date-range-selector/171201/4 "2009-06-30T01:04:32Z")

</div>

Matt, good call there mate.

I created some constants for each week start and end:

WEEK\_ONE\_START = “2009-06-21”  
WEEK\_ONE\_END = “2009-06-27”

And then defined that in a per search scope:

named\_scope :compiled\_week\_one, lambda { { :conditions =\> ['compiled\_on

> ? and compiled\_on \< ?’, WEEK\_TWO\_START, WEEK\_TWO\_END] } }

And can do that for each of the weeks of the season (13 in total). I  
also have a default scope using Time.now.beginning\_of\_week and  
Time.now\_end\_of\_week for current records but the form search allows the  
new scopes and all works well.

Many thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [June 30, 2009, 1:05am UTC](https://www.ruby-forum.com/t/question-about-a-date-range-selector/171201/5 "2009-06-30T01:05:29Z")

</div>

Ã„lphÃ¤ BlÃ¼Ã« wrote:

> And then defined that in a per search scope:
> 
> named\_scope :compiled\_week\_one, lambda { { :conditions =\> ['compiled\_on
> 
> > ? and compiled\_on \< ?’, WEEK\_TWO\_START, WEEK\_TWO\_END] } }

Actually reads WEEK\_ONE\_START and WEEK\_ONE\_END…
