# Simple thing in PHP, a braincracker for me in Ruby

**URL:** <https://www.ruby-forum.com/t/simple-thing-in-php-a-braincracker-for-me-in-ruby/104391>\
**Category:** Rails\
**Created:** [May 17, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/simple-thing-in-php-a-braincracker-for-me-in-ruby/104391 "2007-05-17T14:01:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reinier\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@Reinier\_L](https://www.ruby-forum.com/u/Reinier_L)\
**Post date:** [May 17, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/simple-thing-in-php-a-braincracker-for-me-in-ruby/104391/1 "2007-05-17T14:01:08Z")

</div>

Hi,

I have a PHP background, and learning Ruby is great fun. I want to  
build a mini app that I already have in PHP to Ruby. It’s a personal  
planner, where I plan days (the days are chopped in three pieces 09:00

- 12:00, 13:00 - 18:00 and 20:00 - 22:00)

I want to make an Array or Hash (what do you advise?) that looks like  
this:

my\_data = {  
‘2007’ =\> {

```
    '21' => {
        'mo' => {
            '0' => 'This is my description',
            '1' => 'This is my other description',
            '2' => 'Another description'
        },

        'fr' => {
            '0' => 'Friday I\'m in love!'
        }
    },

    '22' => {
        'mo' => {
            '0' => 'This is my description',
            '1' => 'This is my other description',
            '2' => 'Another description'
        },

        'fr' => {
            '0' => 'Friday I\'m in love!'
        }
    }

},

'2008' => {

    '1' => {

    }

}

```

}

This is what I have from my database:

rb: @chunks = Chunk.find(:all)

I have this in @chunks:

- !ruby/object:Chunk  
attributes:  
part: “0”  
week: “21”  
id: “1”  
description: naar school  
day: ma  
year: “2007”
- !ruby/object:Chunk  
attributes:  
part: “1”  
week: “21”  
id: “2”  
description: naar school  
day: ma  
year: “2007”
- !ruby/object:Chunk  
attributes:  
part: “0”  
week: “21”  
id: “3”  
description: NTU  
day: vr  
year: “2007”
- !ruby/object:Chunk  
attributes:  
part: “0”  
week: “22”  
id: “4”  
description: Pinksteren  
day: ma  
year: “2007”
- !ruby/object:Chunk  
attributes:  
part: “1”  
week: “22”  
id: “5”  
description: Pinksteren  
day: ma  
year: “2007”
- !ruby/object:Chunk  
attributes:  
part: “2”  
week: “22”  
id: “6”  
description: Pinksteren  
day: ma  
year: “2007”
- !ruby/object:Chunk  
attributes:  
part: “1”  
week: “21”  
id: “7”  
description: NTU  
day: vr  
year: “2007”

====

explanation:

```
part = part of the day
week = weeknumber
id = id of the record
description = the description
day: day of the week
year = year

```

So how do I get the information from the database so that I can easily  
loopm trough it in my view? This is how i’ve done it in PHP:

if(is\_array($chunk)){  
foreach($chunk as $row){  
$aData[$row[‘year’]][$row[‘week’]][$row[‘day’]][$row[‘part’]][‘description’]  
= $row[‘omschrijving’];  
$aData[$row[‘year’]][$row[‘week’]][$row[‘day’]][$row[‘part’]][‘id’]  
= $row[‘id’];  
}  
}

How can I accomplish that in Ruby?

–  
Met vriendelijke groet,

Reinier L.  
Digital Energy BV  
W: www.digitalenergy.nl  
T: +31624872725  
E: removed\_email\_address@domain.invalid  
Zin in een enorme lading web 2.0? Check uwbatterij.nl

---

<div class="post-metadata">

**Author:** ![Reinier\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@Reinier\_L](https://www.ruby-forum.com/u/Reinier_L)\
**Post date:** [May 17, 2007, 3:35pm UTC](https://www.ruby-forum.com/t/simple-thing-in-php-a-braincracker-for-me-in-ruby/104391/2 "2007-05-17T15:35:24Z")

</div>

Dag!

I really don’t see a point in taking your array of Chunk objects and  
transforming in to some other deep Array/Hash structure when you have  
object. (This is very PHPish 🙂

You can get them in the right order  
@chunks = Chunk.find(:all, :order =\> ‘field1 asc, field2 desc’)

and the iterate it in your view:

\<% @chunks.each{|chunk| %\>

\<%= chunk.week%\>  
\<%= chunk.whatever%\>

\<% } %\>

Gemakellijk. Or haven’t I understood you correctly?

Cheers,  
Yuri

On 5/17/07, Reinier L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> my\_data = {  
> ‘0’ =\> ‘Friday I'm in love!’  
> ‘fr’ =\> {  
> }
> 
> - !ruby/object:Chunk  
> week: “21”  
> day: vr  
> attributes:  
> id: “6”  
> year: “2007”  
> year = year  
> }  
> T: +31624872725  
> E: removed\_email\_address@domain.invalid  
> Zin in een enorme lading web 2.0? Check uwbatterij.nl
> 
> >

–  
Best regards,  
Yuri L.

---

<div class="post-metadata">

**Author:** ![Reinier\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@Reinier\_L](https://www.ruby-forum.com/u/Reinier_L)\
**Post date:** [May 17, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/simple-thing-in-php-a-braincracker-for-me-in-ruby/104391/3 "2007-05-17T16:14:41Z")

</div>

Hoi,

Well, I want to make tables for every week with it, ordered bij year  
and week. Fot that, I have to loop trough all the available years,  
within the year loop trought the available weeks, and within the week  
loop trough days and parts of days.

I don’t see a simple way to do that with the object I already have.  
Maybe I’m thinking too hard about it, my brain is stuck in PHP 🙂

–  
Met vriendelijke groet,

Reinier L.  
Digital Energy BV  
W: www.digitalenergy.nl  
T: +31624872725  
E: removed\_email\_address@domain.invalid  
Zin in een enorme lading web 2.0? Check uwbatterij.nl

---

<div class="post-metadata">

**Author:** ![Reinier\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@Reinier\_L](https://www.ruby-forum.com/u/Reinier_L)\
**Post date:** [May 17, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/simple-thing-in-php-a-braincracker-for-me-in-ruby/104391/4 "2007-05-17T20:08:33Z")

</div>

> chunk\_table = chunks.group\_by(&:year)

I really meant index\_by here, not group\_by

---

<div class="post-metadata">

**Author:** ![Reinier\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@Reinier\_L](https://www.ruby-forum.com/u/Reinier_L)\
**Post date:** [May 17, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/simple-thing-in-php-a-braincracker-for-me-in-ruby/104391/5 "2007-05-17T20:05:30Z")

</div>

Hoi,

Then you are right, you can use hashes and arrays as one of possible  
variants. Just store your ActiveRecord objects as leaves of this  
structure, not strings and integers.

If you were to just group the objects by year or any other single  
attribute, it could be really easy in Rails:

chunks = Chunk.find(:all)

chunk\_table = chunks.group\_by(&:year)

:year here is an attribute of a Chunk object, the result of this  
shortcut is a hash where keys are unique year values, and values are  
arrays of Chunk objects with this year value.

Cool, isn’t it? 🙂

But yes, in you case you probably need a hash of hashes, something like  
this:

table = Hash.new  
@chunks.each{|chunk|  
table[chunk.year] = Hash.new unless table.has\_key? chunk.year  
table[chunk.year][chunk.week] = Array.new unless  
table[chunk.year].has\_key? chunk.week

```
table[chunk.year][chunk.week] << chunk

```

}

The result is a hash of hashes of arrays of Chunk objects  
This is just an example for you to build on. And I think there are  
other variants.

Groeten,  
Yuri

On 5/17/07, Reinier L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 5/17/07, Yuri L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > and the iterate it in your view:  
> > Cheers,
> > 
> > > - 12:00, 13:00 - 18:00 and 20:00 - 22:00)  
> > > ‘2’ =\> ‘Another description’  
> > > ‘1’ =\> ‘This is my other description’,  
> > > ‘2008’ =\> {
> > > 
> > > - !ruby/object:Chunk  
> > > week: “21”  
> > > week = weeknumber  
> > > $aData[$row[‘year’]][$row[‘week’]][$row[‘day’]][$row[‘part’]][‘description’]
> > 
> > –  
> > Best regards,  
> > Yuri L.
> > 
> > >
> 
> >

–  
Best regards,  
Yuri L.
