Hash is not merged / recursive function

Hi,

i’m new to ruby, but i really like it.

I’m trying to extend Ruby on Rails FormHelper with a custom function
which returns a recursive tree select field for “acts_as_tree”-models.

My Problem is, that the hash is not being merged correctly and so i only
get values which are up to the root of the tree.

Thanks for all answers.

Here’s my code:

module ActionView
module Helpers
class FormBuilder
def tree_select_options(collection, value_method, text_method,
level = 0)
choices = {}
collection.map do |element|
choices[" " * level + element.send(text_method)] =
element.send(value_method)
if (element.children.size > 0)
choices.merge(tree_select_options(element.children,
value_method, text_method, level+5))
end
end
return choices
end

  def tree_select(method, collection, value_method, text_method,

options = {}, html_options = {})
choices = tree_select_options(collection, value_method,
text_method)
@template.select(@object_name, method, choices,
options.merge(:object => @object), html_options)
end
end
end
end

On Mon, Jan 5, 2009 at 3:35 PM, Dominik Krebs
[email protected] wrote:
I have no idea what’s wrong with your code, but I happen to know a
Ruby Guru, so I asked him for help, here is what he told me, hopefully
that will enlighten you :wink:

504/7 > irb
irb(main):001:0> options={:value => 42}
=> {:value=>42}
irb(main):002:0> options.update( :answer => 42 )
=> {:value=>42, :answer=>42}
irb(main):003:0> options
=> {:value=>42, :answer=>42}
irb(main):004:0> options.merge( :wisdom => 42 )
=> {:value=>42, :answer=>42, :wisdom=>42}
irb(main):005:0> options
=> {:value=>42, :answer=>42}
irb(main):006:0> options.merge!( :wisdom => 42 )
=> {:value=>42, :answer=>42, :wisdom=>42}
irb(main):007:0> options
=> {:value=>42, :answer=>42, :wisdom=>42}

HTH
Robert