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 -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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") -- Posted via http://www.ruby-forum.com/. -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- Posted via http://www.ruby-forum.com/. -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Feb 3, 2010 at 3:58 AM, eggie5 <eggie5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Feb 4, 4:08 am, Rick DeNatale <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Wed, Feb 3, 2010 at 3:58 AM, eggie5 <egg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Wed, Feb 3, 2010 at 3:58 AM, eggie5 <egg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.