Heya, I think I have thinking barrier. I just want a simple hash out of an ActiveRecord. @attributes = Attribute.find_all_by_character_id(@character).hash { |u| [u.name, u.value] } and I would like to access it like @attributes[:health] but it doesn''t work. Anyone can help me out with that? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Try this: @attributes = Hash[*Attribute.find_all_by_character_id(@character).map { |a| [a.name, a.value] }.flatten] On Mar 25, 7:46 am, Heinz Strunk <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Heya, > > I think I have thinking barrier. I just want a simple hash out of an > ActiveRecord. > > @attributes = Attribute.find_all_by_character_id(@character).hash { > |u| [u.name, u.value] } > > and I would like to access it like > > @attributes[:health] > > but it doesn''t work. Anyone can help me out with that? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Mar 25, 11:46 am, Heinz Strunk <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > @attributes = Attribute.find_all_by_character_id(@character).hash { > |u| [u.name, u.value] } > > and I would like to access it like > > @attributes[:health] >> but it doesn''t work. Anyone can help me out with that?The keys of your hash are strings, not symbols. (be careful with an instance variable called @attributes if this is in an instance method - you would overwrite activerecord''s instance variable of the same name) Fred> -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I actually don''t get the OP''s use of the hash method in this context: http://www.ruby-doc.org/core/classes/Array.html#M002182 It seems that it''s just computing the hash, and the block passed is just being silently ignored... Heniz, using your code what''s the output of @attributes.inspect and @attributes.class.name? On Mar 25, 8:33 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 25, 11:46 am, Heinz Strunk <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > > > @attributes = Attribute.find_all_by_character_id(@character).hash { > > |u| [u.name, u.value] } > > > and I would like to access it like > > > @attributes[:health] > > > but it doesn''t work. Anyone can help me out with that? > > The keys of your hash are strings, not symbols. (be careful with an > instance variable called @attributes if this is in an instance method > - you would overwrite activerecord''s instance variable of the same > name) > > Fred > > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I just renamed @attributes but using ''health'' instead of :health doesn''t really help... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Mar 25, 12:48 pm, Heinz Strunk <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I just renamed @attributes but using ''health'' instead of :health doesn''t > really help...There''s also what Harold said - that hash method isn''t doing what you think it is. Fred> -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Mar 25, 12:48�pm, Heinz Strunk <rails-mailing-l...@andreas-s.net> > wrote: >> I just renamed @attributes but using ''health'' instead of :health doesn''t >> really help... > > There''s also what Harold said - that hash method isn''t doing what you > think it is. > > FredOh yeah, I overlooked his first post. Works like a charm, thanks :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---