Forum: Ruby on Rails Hash#merge in a controller

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.
D0bb06c137d862bffd9a76500c8d2366?d=identicon&s=25 Alberto Santini (asan)
on 2009-05-09 13:58
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 :-)
Thanks in advance.
D0bb06c137d862bffd9a76500c8d2366?d=identicon&s=25 Alberto Santini (asan)
on 2009-05-09 15:19
Mistery solved: that wasn't an Hash, but an HashWithIndifferentAccess,
which #merge method doesn't accept any block :-)
A .to_hash was sufficient :-)
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-05-11 03:30
(Received via mailing list)
On 09/05/2009, at 9:58 PM, Alberto Santini
<rails-mailing-list@andreas-s.net
 > 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/
Learn: http://sensei.zenunit.com/
Twitter: http://twitter.com/random8r
This topic is locked and can not be replied to.