Simple thing in PHP, a braincracker for me in Ruby

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: [email protected]
Zin in een enorme lading web 2.0? Check uwbatterij.nl

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 :slight_smile:

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. [email protected] 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: [email protected]
    Zin in een enorme lading web 2.0? Check uwbatterij.nl


Best regards,
Yuri L.

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 :slight_smile:


Met vriendelijke groet,

Reinier L.
Digital Energy BV
W: www.digitalenergy.nl
T: +31624872725
E: [email protected]
Zin in een enorme lading web 2.0? Check uwbatterij.nl

chunk_table = chunks.group_by(&:year)

I really meant index_by here, not group_by

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? :slight_smile:

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. [email protected] wrote:

On 5/17/07, Yuri L. [email protected] 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’ => {

    year: “2007”
    part: “0”
    description: Pinksteren

  • !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.