# Hash with key and value from ActiveRecord?

**URL:** <https://discuss.rubyonrails.org/t/hash-with-key-and-value-from-activerecord/40850>\
**Category:** rubyonrails-talk\
**Created:** [March 25, 2009, 11:46am UTC](https://discuss.rubyonrails.org/t/hash-with-key-and-value-from-activerecord/40850 "2009-03-25T11:46:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 25, 2009, 11:46am UTC](https://discuss.rubyonrails.org/t/hash-with-key-and-value-from-activerecord/40850/1 "2009-03-25T11:46:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Harold](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@Harold](https://discuss.rubyonrails.org/u/Harold)\
**Post date:** [March 25, 2009, 12:30pm UTC](https://discuss.rubyonrails.org/t/hash-with-key-and-value-from-activerecord/40850/2 "2009-03-25T12:30:40Z")

</div>

Try this:

@attributes = Hash[\*Attribute.find\_all\_by\_character\_id(@character).map { |a| [a.name, a.value] }.flatten]

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [March 25, 2009, 12:33pm UTC](https://discuss.rubyonrails.org/t/hash-with-key-and-value-from-activerecord/40850/3 "2009-03-25T12:33:17Z")

</div>

> @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

---

<div class="post-metadata">

**Author:** ![Harold](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@Harold](https://discuss.rubyonrails.org/u/Harold)\
**Post date:** [March 25, 2009, 12:42pm UTC](https://discuss.rubyonrails.org/t/hash-with-key-and-value-from-activerecord/40850/4 "2009-03-25T12:42:45Z")

</div>

I actually don't get the OP's use of the hash method in this context:

> **[Class: Array (Ruby 3.1.2)](https://ruby-doc.org/core-3.1.2/Array.html)**
>
> Class : Array - Ruby 3.1.2

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?

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 25, 2009, 12:48pm UTC](https://discuss.rubyonrails.org/t/hash-with-key-and-value-from-activerecord/40850/5 "2009-03-25T12:48:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [March 25, 2009, 1:01pm UTC](https://discuss.rubyonrails.org/t/hash-with-key-and-value-from-activerecord/40850/6 "2009-03-25T13:01:40Z")

</div>

> 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
