# A find to key pair

**URL:** <https://www.ruby-forum.com/t/a-find-to-key-pair/140178>\
**Category:** Rails\
**Created:** [June 11, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/a-find-to-key-pair/140178 "2008-06-11T16:23:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [June 11, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/a-find-to-key-pair/140178/1 "2008-06-11T16:23:39Z")

</div>

I would like to get an array/hash where I can write @image[88] and that  
would result in “thisimage.jpg”. I’ve got:

@images = Image.find(:all, :select =\> “id, real\_file\_name”)  
result = {}  
@images.map {|a| result[a.id] = a.real\_file\_name }

I think this is correct, but I don’t know how to get the result from it.  
It doesn’t work with @images[88]. What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [June 11, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/a-find-to-key-pair/140178/2 "2008-06-11T22:37:33Z")

</div>

On Jun 11, 2008, at 7:23 AM, Pål Bergström wrote:

> It doesn’t work with @images[88]. What am I doing wrong?  
> There may be a more railsey way, but this is a basic Ruby way that  
> should work

@images = Image.find(:all, :select =\> “id, real\_file\_name”)  
@image\_map = {} # \<— should be a better name for this  
@images.each do |this\_image|  
@image\_map.store(this\_image.id, this\_image.real\_file\_name)  
end

@image\_map[some\_id]

– gw

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [June 11, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/a-find-to-key-pair/140178/3 "2008-06-11T23:02:49Z")

</div>

Greg W. wrote:

> On Jun 11, 2008, at 7:23 AM, Pï¿½l Bergstrï¿½m wrote:
> 
> > It doesn’t work with @images[88]. What am I doing wrong?  
> > There may be a more railsey way, but this is a basic Ruby way that  
> > should work
> 
> @images = Image.find(:all, :select =\> “id, real\_file\_name”)  
> @image\_map = {} # \<— should be a better name for this  
> @images.each do |this\_image|  
> @image\_map.store(this\_image.id, this\_image.real\_file\_name)  
> end
> 
> @image\_map[some\_id]
> 
> – gw

Great. Thanks 🙂
