Another question related to this... I have everything grouped by common date, but now I want to do it my month. Any ideas?
On Feb 4, 4:08 am, Rick DeNatale <rick.denat...@gmail.com> wrote: > On Wed, Feb 3, 2010 at 3:58 AM, eggie5 <egg...@gmail.com> 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. > > 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/ > Twitter:http://twitter.com/RickDeNatale > WWR:http://www.workingwithrails.com/person/9021-rick-denatale > LinkedIn:http://www.linkedin.com/in/rickdenatale -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.