# Hash#merge in a controller

**URL:** <https://www.ruby-forum.com/t/hash-merge-in-a-controller/167561>\
**Category:** Rails\
**Created:** [May 9, 2009, 1:58pm UTC](https://www.ruby-forum.com/t/hash-merge-in-a-controller/167561 "2009-05-09T13:58:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![albertosesena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@albertosesena](https://www.ruby-forum.com/u/albertosesena)\
**Post date:** [May 9, 2009, 1:58pm UTC](https://www.ruby-forum.com/t/hash-merge-in-a-controller/167561/1 "2009-05-09T13:58:00Z")

</div>

Hi all!

I have a Place model with a description attribute and a Service model  
with an itinerary attribute. An itinerary is simply a string with  
dash-seprated place descriptions. In my services\_controller.rb#create i  
have the following code:

itinerary = params[:places].merge(params[:places]){|id, place\_id|  
Place.find(place\_id).description}

Where params[:places] is a hash like

params[:places] #=\> {“1” =\> “3”, “2” =\> “4”, “3” =\> “3”}

in the form

{:leg\_number =\> :place\_id}

I would like to receive a Hash like the following

itinerary #=\> {“1” =\> “San Francisco”, “2” =\> “Los A.”, “3” =\> “San  
Francisco”}

and I use the code above to achieve that.

The code works well in script/console, while it is not working in the  
controller, producing the same params[:places] array, like if the block  
is not excuted.  
I mean, in my controller, if I put the above code or the following

itinerary = params[:places].merge(params[:places])

i get the same result:

itinerary #=\> {“1” =\> “3”, “2” =\> “4”, “3” =\> “3”}

Where am I wrong? Any suggestion is appreciated 🙂  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![albertosesena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@albertosesena](https://www.ruby-forum.com/u/albertosesena)\
**Post date:** [May 9, 2009, 3:19pm UTC](https://www.ruby-forum.com/t/hash-merge-in-a-controller/167561/2 "2009-05-09T15:19:30Z")

</div>

Mistery solved: that wasn’t an Hash, but an HashWithIndifferentAccess,  
which #merge method doesn’t accept any block 🙂  
A .to\_hash was sufficient 🙂

---

<div class="post-metadata">

**Author:** ![albertosesena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@albertosesena](https://www.ruby-forum.com/u/albertosesena)\
**Post date:** [May 11, 2009, 3:30am UTC](https://www.ruby-forum.com/t/hash-merge-in-a-controller/167561/3 "2009-05-11T03:30:39Z")

</div>

On 09/05/2009, at 9:58 PM, Alberto S.  
\<removed\_email\_address@domain.invalid

> wrote:

> Hi all!
> 
> I have a Place model with a description attribute and a Service model  
> with an itinerary attribute. An itinerary is simply a string with  
> dash-seprated place descriptions. In my  
> services\_controller.rb#create i  
> have the following code:

Hi. This whole email is really confusing. I suggest you try to send it  
again, but constrain yourself to being very simple and using half the  
length. Explain every step!

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)
