Design Pattern

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

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")

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,

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.

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.

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

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).

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