Hi - What is the best way to create a nested hash from an array of objects? Here''s what I''m trying to do, take my array of objects and use the attribute ''position'' as a key for each nested hash. The nested hash will contain the other array attributes. Example: [#<Spec id: 4057, position: 1, name: "Tonnage", english_unit: "T", metric_unit: "t">, [#<Spec id: 4058, position: 2, name: "Spindle", english_unit: "T", metric_unit: "t">] This is what I want to end up with from the above: {1 => {:name => "Tonnage", :english_unit => "T", :metric_unit => "t"}, 2 => {:name => "Spindle", :english_unit => "T", :metric_unit => "t"}} I''ve tried different methods of enumerating but can''t quite get what I want. Thanks for any help! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Looks like you''re starting with ActiveRecord''s so you could possibly take advantage of: spec_hash = Spec.find(4057).attributes and glue something together. On Feb 19, 3:54 pm, northband <northb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi - > > What is the best way to create a nested hash from an array of objects? > > Here''s what I''m trying to do, take my array of objects and use the > attribute ''position'' as a key for each nested hash. The nested hash > will contain the other array attributes. > > Example: > > [#<Spec id: 4057, position: 1, name: "Tonnage", english_unit: "T", > metric_unit: "t">, > [#<Spec id: 4058, position: 2, name: "Spindle", english_unit: "T", > metric_unit: "t">] > > This is what I want to end up with from the above: > > {1 => {:name => "Tonnage", :english_unit => "T", :metric_unit => "t"}, > 2 => {:name => "Spindle", :english_unit => "T", :metric_unit => "t"}} > > I''ve tried different methods of enumerating but can''t quite get what I > want. > > Thanks for any help!--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nice - thanks for the tip. I got this working great now. On Feb 19, 11:02 pm, Rick <Richard.T.Ll...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Looks like you''re starting with ActiveRecord''s so you could possibly > take advantage of: > > spec_hash = Spec.find(4057).attributes > > and glue something together. > > On Feb 19, 3:54 pm, northband <northb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi - > > > What is the best way to create a nested hash from an array of objects? > > > Here''s what I''m trying to do, take my array of objects and use the > > attribute ''position'' as a key for each nested hash. The nested hash > > will contain the other array attributes. > > > Example: > > > [#<Spec id: 4057, position: 1, name: "Tonnage", english_unit: "T", > > metric_unit: "t">, > > [#<Spec id: 4058, position: 2, name: "Spindle", english_unit: "T", > > metric_unit: "t">] > > > This is what I want to end up with from the above: > > > {1 => {:name => "Tonnage", :english_unit => "T", :metric_unit => "t"}, > > 2 => {:name => "Spindle", :english_unit => "T", :metric_unit => "t"}} > > > I''ve tried different methods of enumerating but can''t quite get what I > > want. > > > Thanks for any help!--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---