# Design Pattern

**URL:** <https://www.ruby-forum.com/t/design-pattern/182824>\
**Category:** Rails\
**Created:** [February 3, 2010, 10:00am UTC](https://www.ruby-forum.com/t/design-pattern/182824 "2010-02-03T10:00:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [February 3, 2010, 10:00am UTC](https://www.ruby-forum.com/t/design-pattern/182824/1 "2010-02-03T10:00:21Z")

</div>

Hi,

I have a collection of arbitrary objects that have a date attribute. I  
want to display these objects by month. For example

@things=Thing.all

Now I want to display in a table all the things by month - how can I  
do this? the problem isn’t making a table or anything it’s just the  
logic for getting the min and max range for all the dates in things  
and then finding the monthes to use for the scale and etc. Please give  
suggestions.

Thanks,  
Elder Egg

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [February 3, 2010, 11:25am UTC](https://www.ruby-forum.com/t/design-pattern/182824/2 "2010-02-03T11:25:34Z")

</div>

To work on the day/month/date portion of a date (mysql) use  
DAY(your\_date) MONTH(your\_date) YEAR(your\_date).

For instance  
@things = Thing.all(:conditions =\> “YEAR(created\_at)=2009”)

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [February 3, 2010, 3:18pm UTC](https://www.ruby-forum.com/t/design-pattern/182824/3 "2010-02-03T15:18:24Z")

</div>

Sharagoz – wrote:

> To work on the day/month/date portion of a date (mysql)

Not just mySQL. This is standard SQL syntax.

> use  
> DAY(your\_date) MONTH(your\_date) YEAR(your\_date).
> 
> For instance  
> @things = Thing.all(:conditions =\> “YEAR(created\_at)=2009”)

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [February 3, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/design-pattern/182824/4 "2010-02-03T17:32:10Z")

</div>

eggie5 wrote:

> Hi,
> 
> I have a collection of arbitrary objects that have a date attribute. I  
> want to display these objects by month. For example
> 
> @things=Thing.all
> 
> Now I want to display in a table all the things by month - how can I  
> do this? the problem isn’t making a table or anything it’s just the  
> logic for getting the min and max range for all the dates in things  
> and then finding the monthes to use for the scale and etc. Please give  
> suggestions.

Well…

@things = @things.sort\_by { |a, b| a.date \<=\> b.date }

That’ll work to sort if the date attribute has the same name on all  
objects.  
From there, you can also do “select” if a.date.month == 1, etc etc etc.

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [February 6, 2010, 4:55am UTC](https://www.ruby-forum.com/t/design-pattern/182824/5 "2010-02-06T04:55:18Z")

</div>

Thanks, this is what I was looking for… not just a flat collection  
sorted by date but sorted by date with sub objects ( I don’t know how  
to explain better).

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [February 8, 2010, 1:04pm UTC](https://www.ruby-forum.com/t/design-pattern/182824/6 "2010-02-08T13:04:34Z")

</div>

Another question related to this… I have everything grouped by  
common date, but now I want to do it my month. Any ideas?

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [February 3, 2010, 8:10pm UTC](https://www.ruby-forum.com/t/design-pattern/182824/7 "2010-02-03T20:10:14Z")

</div>

On Wed, Feb 3, 2010 at 3:58 AM, eggie5 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and then finding the monthes to use for the scale and etc. Please give  
> suggestions.

Unlike some of the other responders, I’m going to take your “arbitray  
objects” to mean that they aren’t necessarily ActiveRecord models, and  
therefore SQL solutions don’t apply. Assuming that each of these Ruby  
objects has a method date which returns either a Date, DateTime, or  
Time object then something like

@things.group\_by {|thing| thing.date.to\_date.beginning\_of\_month }

while return a hash whose keys are the dates which have at least one  
thing, and whose values are arrays of things whose dates fall within  
the month.

If you know that the things all will return a Date from .date then you  
can leave out the to\_date in the expression.

> > def initialize(date)  
> > @date = date  
> > end  
> > attr\_reader :date  
> > end  
> > =\> nil

> > things = [Date.today, 2.days.from\_now, 5.days.ago].map {|d| Thing.new(d)}  
> > =\> [#\<Thing:0x00000101524828 @date=Wed, 03 Feb 2010\>,  
> > #\<Thing:0x000001015247f0 @date=2010-02-05 14:06:22 -0500\>,  
> > #\<Thing:0x00000101524780 @date=2010-01-29 14:06:22 -0500\>]

> > things.group\_by {|t| t.date.to\_date.beginning\_of\_month}  
> > =\> {  
> > Mon, 01 Feb 2010=\>[#\<Thing:0x00000101524828 @date=Wed, 03 Feb  
> > 2010\>, #\<Thing:0x000001015247f0 @date=2010-02-05 14:06:22 -0500\>],  
> > Fri, 01 Jan 2010=\>[#\<Thing:0x00000101524780 @date=2010-01-29 14:06:22  
> > -0500\>]  
> > }

## HTH

Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [Rick DeNatale - Developer - IBM | LinkedIn](http://www.linkedin.com/in/rickdenatale)
