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
on 2010-02-03 10:00
on 2010-02-03 11:25
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")
on 2010-02-03 15:18
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 marnen@marnen.org
on 2010-02-03 17:32
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.
on 2010-02-03 20:10
On Wed, Feb 3, 2010 at 3:58 AM, eggie5 <eggie5@gmail.com> 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/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale
on 2010-02-06 04:55
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).
on 2010-02-08 13:04
Another question related to this... I have everything grouped by common date, but now I want to do it my month. Any ideas?
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.