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"

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

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:

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

chunk_table = chunks.group_by(&:year)

I really meant index_by here, not group_by