# Need help figuring out this code block

**URL:** <https://www.ruby-forum.com/t/need-help-figuring-out-this-code-block/129348>\
**Category:** Rails\
**Created:** [February 17, 2008, 7:04am UTC](https://www.ruby-forum.com/t/need-help-figuring-out-this-code-block/129348 "2008-02-17T07:04:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [February 17, 2008, 7:04am UTC](https://www.ruby-forum.com/t/need-help-figuring-out-this-code-block/129348/1 "2008-02-17T07:04:28Z")

</div>

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 %\>

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [February 17, 2008, 8:18am UTC](https://www.ruby-forum.com/t/need-help-figuring-out-this-code-block/129348/2 "2008-02-17T08:18:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [February 18, 2008, 1:47am UTC](https://www.ruby-forum.com/t/need-help-figuring-out-this-code-block/129348/3 "2008-02-18T01:47:07Z")

</div>

Are you trying to output a hash (as text)?

On Feb 17, 1:04 am, Robert M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [February 18, 2008, 1:49am UTC](https://www.ruby-forum.com/t/need-help-figuring-out-this-code-block/129348/4 "2008-02-18T01:49:02Z")

</div>

AndyV wrote:

> Are you trying to output a hash (as text)?
> 
> On Feb 17, 1:04 am, Robert M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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.

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [February 18, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/need-help-figuring-out-this-code-block/129348/5 "2008-02-18T20:00:27Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
