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 Ladan Digital Energy BV W: www.digitalenergy.nl T: +31624872725 E: reinier-eFwX6J65rk81SCDwVaL6kg@public.gmane.org Zin in een enorme lading web 2.0? Check uwbatterij.nl --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Ladan <reinierl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 Ladan > Digital Energy BV > W: www.digitalenergy.nl > T: +31624872725 > E: reinier-eFwX6J65rk81SCDwVaL6kg@public.gmane.org > Zin in een enorme lading web 2.0? Check uwbatterij.nl > > > >-- Best regards, Yuri Leikind --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Reinier Ladan
2007-May-17 14:13 UTC
Re: Simple thing in PHP, a braincracker for me in Ruby
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 Ladan Digital Energy BV W: www.digitalenergy.nl T: +31624872725 E: reinier-eFwX6J65rk81SCDwVaL6kg@public.gmane.org Zin in een enorme lading web 2.0? Check uwbatterij.nl On 5/17/07, Yuri Leikind <yuri.leikind-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 Ladan <reinierl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > 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 Ladan > > Digital Energy BV > > W: www.digitalenergy.nl > > T: +31624872725 > > E: reinier-eFwX6J65rk81SCDwVaL6kg@public.gmane.org > > Zin in een enorme lading web 2.0? Check uwbatterij.nl > > > > > > > > > > -- > Best regards, > Yuri Leikind > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Ladan <reinierl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 Ladan > Digital Energy BV > W: www.digitalenergy.nl > T: +31624872725 > E: reinier-eFwX6J65rk81SCDwVaL6kg@public.gmane.org > Zin in een enorme lading web 2.0? Check uwbatterij.nl > > On 5/17/07, Yuri Leikind <yuri.leikind-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > 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 Ladan <reinierl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > 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 Ladan > > > Digital Energy BV > > > W: www.digitalenergy.nl > > > T: +31624872725 > > > E: reinier-eFwX6J65rk81SCDwVaL6kg@public.gmane.org > > > Zin in een enorme lading web 2.0? Check uwbatterij.nl > > > > > > > > > > > > > > > > -- > > Best regards, > > Yuri Leikind > > > > > > > > > > >-- Best regards, Yuri Leikind --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> chunk_table = chunks.group_by(&:year) >I really meant index_by here, not group_by --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---