To_json of hash containing an array

Hi there,

I am fairly new to the Rails framework, so forgive me if there is an
obvious solution to this problem.

I am using Rails 2.1.0 and trying to jsonify a hash containing an
array. E.g.

result = { :name => “McLovin”, :attachments => [‘file.pdf’, ‘file.jpg’] }
=> {:attachments=>[“file.pdf”, “file.jpg”], :name=>“McLovin”}

result.to_json

I would expect something like:

{
“name”: “McLovin”,
“attachments”: [
“file.pdf”,
“file.jpg”
]
}

But instead I get:

TypeError: wrong argument type Hash (expected Data)
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/
active_support/json/encoding.rb:21:in to_json' from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/ active_support/json/encoding.rb:21:insend’
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/
active_support/json/encoding.rb:21:in encode' from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/ active_support/json/encoding.rb:31:inraise_on_circular_reference’
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/
active_support/json/encoding.rb:20:in encode' from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/ active_support/json/encoders/hash.rb:42:into_json’
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/
active_support/json/encoders/hash.rb:41:in map' from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/ active_support/json/encoders/hash.rb:41:into_json’
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/
active_support/core_ext/object/misc.rb:28:in returning' from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/ active_support/json/encoders/hash.rb:40:into_json’
from (irb):54

If I do “result.to_json( :except => :attachments )”, then it works.

Best regards, Mads K.