I posted this on the Ruby board but it may belong here. I have a piece of code below. I have activities hooked up to a Calendar but I''m running into a problem I can''t wrap my head around. What I''m trying to achieve is the output: { period: ''<%= February 17, 2008 %>'', teaser: [''Activity 1'', ''Activity 2''], label: ['''']}, if there is more than on activity for a given day. I was thinking that I would have to dump the information into an array and iterator within the teaser: line, but I can''t quite figure it. Any help would be appreciated. Thanks! <% @projects.each do |a| %> <% a.activities.each do |b| %> { period: ''<%= b.win_at.strftime("%B, %d, %Y") %>'', teaser: [''<%b.name %>''], label: ['''']}, <% end %> <% end %> -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robert Malko wrote:> I posted this on the Ruby board but it may belong here. > > Thanks! > > <% @projects.each do |a| %> > <% a.activities.each do |b| %> > { period: ''<%= b.win_at.strftime("%B, %d, %Y") %>'', teaser: [''<%> b.name %>''], label: ['''']}, > <% end %> > <% end %>You didn''t tell us what was going wrong or what part of it is working and what part not. When I have trouble with something like this with some wacky syntax, I like to hide as much of it inside other strings as possible to simplify the line that is troublesome. I would refactor that middle line into something like <% front = "{ period: \''" %> <% middle = "\'', teaser: [\''" %> <% back = "\''], label: [\''\'']}," <%= front + b.win_at.strftime(("%B, %d, %Y") + middle + b.name + back %> its easier to follow, and it might either solve your problem or make it easier to find it. HTH, jp -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Are you trying to output a hash (as text)? On Feb 17, 1:04 am, Robert Malko <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I posted this on the Ruby board but it may belong here. > > I have a piece of code below. I have activities hooked up to a Calendar > but I''m running into a problem I can''t wrap my head around. What I''m > trying to achieve is the output: { period: ''<%= February 17, 2008 %>'', > teaser: [''Activity 1'', ''Activity 2''], label: ['''']}, if there is more > than on activity for a given day. > > I was thinking that I would have to dump the information into an array > and iterator within the teaser: line, but I can''t quite figure it. > > Any help would be appreciated. > > Thanks! > > <% @projects.each do |a| %> > <% a.activities.each do |b| %> > { period: ''<%= b.win_at.strftime("%B, %d, %Y") %>'', teaser: [''<%> b.name %>''], label: ['''']}, > <% end %> > <% end %> > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
AndyV wrote:> Are you trying to output a hash (as text)? > > On Feb 17, 1:04 am, Robert Malko <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>I believe so yes, so that way I can get all of the records from the same date into one has so I can iterate through that. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Err... I''m not sure I fully understand what you''re trying to accomplish. I''ll throw out an idea and maybe that will help. @projects.inject(activities = []){|activities, project| activities.push project.activities.find(:all, :conditions=>["win_at>= ?", Date.today])}@current_activities = activities.flatten.group_by{|activity| activity.win_at}.sort The first line uses a find to make sure that you''re dealing with an actual array of activities (rather than an association proxy that looks like one) and the dates you''re dealing with lie in the appropriate date range (modify as you need, obviously). The second line is doing several things. The first line returns an array of arrays, so the call to flatten makes this one big array. Next, group_by iterates over the array and builds a hash, with the hash key being the results of the block; in this case you''ll have a key that looks like a nicely formatted date. Finally, the call to sort *should* cause the hash to be recast as an array and then sorted by the keys (dates). In the end, you should end up with @current_activities being an array sorted in ascending date order. Each entry of @current_activities is an array with the first element being the date and the last element being an array of activities. Over in your view, you can render this out somehow (not sure how you have your calendar set up) but just as an idea... ... <% @current_activities.each do |activity_set| %> <h2> <%= activity_set.first.strftime("%B %d, %Y") %></h2> <ul> <% activity_set.last.each do |activity| %> <li> <%= h activity.teaser %> </li> <% end %> </ul> <% end %> HTH, AndyV On Feb 17, 7:49 pm, Robert Malko <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> AndyV wrote: > > Are you trying to output a hash (as text)? > > > On Feb 17, 1:04 am, Robert Malko <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > I believe so yes, so that way I can get all of the records from the same > date into one has so I can iterate through that. > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---