Hash with key and value from ActiveRecord?

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?

Try this:

@attributes = Hash[*Attribute.find_all_by_character_id(@character).map
{ |a| [a.name, a.value] }.flatten]

@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

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?

I just renamed @attributes but using 'health' instead of :health doesn't
really help...

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