I'm using ActiveResource to use Rails to put a web front end on a
Windows system. The Windows system business objects can serialize
themselves into XML and will accept XML for updates. But...
The default behavior of ActiveResource when saving is to dasherize any
underscores in the tag names and the XML has underscores in it. The
result is that I can read from the other system OK, but when I POST
the changes back most of the field names have been changed by Rails.
I can get round it by using XSLT on the stuff coming back from Rails,
but that's messy and I don't think Rails should be changing the field
names in the first place.
The ActiveResource documentation says that ActiveResource#to_xml and
#encode will default dasherize to false. But they don't. The reason
being that Hash#to_xml and Array#to_xml both default dasherize to
The tests in active_support/core_ext/hash_ext_test.rb and
array_ext_test.rb test :dasherize=true and :dasherize=>false but they
don't test the default, so effectively the default behavior is
The XML examples in the code all show output XML from ActiveRecord
with the field names dasherized, so it might be a policy to do this,
but it seems strange. In general the default behavior would be the one
with "least surprise" and having field names changing without asking
for them to be changed is quite surprising.
It's also annoying because there's no way to change the value
of :dasherize used by ActiveResource#save. My guess is that this has
slipped through because there aren't many people doing system
integration via ActiveResource, otherwise this problem would have been
I've cloned Rails, updated the tests so I've now got some tests which
fail on the old code, I've updated the method "rename_key" which is
the method used by Hash#to_xml and Array#to_xml so that :dasherize now
defaults to false. But there are many other tests which expect it to
default to true.
I can go and change those tests, and also DRY up the Array#to_xml
where it's partially duplicating the code in "rename_key" on the root
element, (but not responding to :camelize) but the documentation
inside array/conversions.rb all expects the output of to_xml to
use :dasherize defaulting to true.
I can clean up the documentation, but this is spreading a bit wide so
I'd appreciate some input from core members.
Is the default behavior of Hash#to_xml and Array#to_xml supposed to
have dasherize true by default and if so why? It seems strange to me
to have the default to change field names. Or is it something that
crept in without people paying much attention to it? The assumption
has spread far and wide.
If it's a policy to default dasherize underscores when converting
ActiveRecord field names and ActiveResource fieldnames to XML can we
have a way to change the default. We'd also need to make the
documentation fit the policy.
My view is that the default should leave things alone,
i.e :dasherize=>false and :camelize=>false. If lots of people have
code that expects the default to flip underscores into hyphens then we
can have a boolean switch setting in Rails initialization which will
set :dasherize=>true for those that have come to expect it.
As Rails becomes more popular and starts to be used in systems
integration then simple "no surprises" behavior when sending and
receiving XML is essential.