Forum: Ruby on Rails Hash with key and value from ActiveRecord?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2009-03-25 12:46
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?
53be54e5db4dc58e4980db5a8255621b?d=identicon&s=25 Harold (Guest)
on 2009-03-25 13:32
(Received via mailing list)
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...@andreas-s.net>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-25 13:34
(Received via mailing list)
On Mar 25, 11:46 am, Heinz Strunk <rails-mailing-l...@andreas-s.net>
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
53be54e5db4dc58e4980db5a8255621b?d=identicon&s=25 Harold (Guest)
on 2009-03-25 13:43
(Received via mailing list)
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...@gmail.com>
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2009-03-25 13:48
I just renamed @attributes but using 'health' instead of :health doesn't
really help...
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-25 14:02
(Received via mailing list)
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.

Fred
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2009-03-25 14:19
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.
>
> Fred

Oh yeah, I overlooked his first post. Works like a charm, thanks :)
This topic is locked and can not be replied to.