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 %>
Robert M. 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
Are you trying to output a hash (as text)?
On Feb 17, 1:04 am, Robert M. [email protected]
AndyV wrote:
Are you trying to output a hash (as text)?
On Feb 17, 1:04 am, Robert M. [email protected]
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.
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| %>
<%= activity_set.first.strftime("%B %d, %Y") %>
<% activity_set.last.each do |activity| %>
- <%= h activity.teaser %>
<% end %>
<% end %>
HTH,
AndyV
On Feb 17, 7:49 pm, Robert M. [email protected]