Hash#merge in a controller

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 Angeles", "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 :slight_smile: Thanks in advance.

Mistery solved: that wasn't an Hash, but an HashWithIndifferentAccess, which #merge method doesn't accept any block :slight_smile: A .to_hash was sufficient :slight_smile:

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/ Learn: http://sensei.zenunit.com/ Twitter: http://twitter.com/random8r