I have some xml that looks like this:
Smith
Jones
In this case,
acct_hash = Hash.from_xml
produces
acct_hash[‘accounts’][‘account’]
which is an Array. But if there is only one account in the xml, as in
Smith
then acct_hash[‘accounts’][‘account’] is a Hash.
So to turn those into Account objects I have to do slightly different
things depending on the number of accounts.
To figure out what to do I’m using
if acct_hash[‘accounts’][‘account’].class.to_s == “Array”
…
which works, but seems ‘wrong.’ Is there a better way?
I wonder why Hash.from_xml doesn’t just always create an Array. I’m
sure there’s a good reason, but it would be simpler in this case.
Thanks,
-George
George B. wrote:
I have some xml that looks like this:
Smith
Jones
In this case,
acct_hash = Hash.from_xml
produces
acct_hash[‘accounts’][‘account’]
which is an Array. But if there is only one account in the xml, as in
Smith
then acct_hash[‘accounts’][‘account’] is a Hash.
So to turn those into Account objects I have to do slightly different
things depending on the number of accounts.
To figure out what to do I’m using
if acct_hash[‘accounts’][‘account’].class.to_s == “Array”
…
which works, but seems ‘wrong.’ Is there a better way?
I wonder why Hash.from_xml doesn’t just always create an Array. I’m
sure there’s a good reason, but it would be simpler in this case.
Yep, that’s annoying and wrong, but it was fixed months ago in edge:
http://dev.rubyonrails.org/changeset/7074
If you upgrade to edge or Rails 2.0 RC1, things will work as you want.
Note that you need the type=“array” attribute in the array entity, like
so:
Smith
–
Josh S.
http://blog.hasmanythrough.com
On Nov 18, 2007, at 1:50 PM, Josh S. wrote:
Yep, that’s annoying and wrong, but it was fixed months ago in edge:
http://dev.rubyonrails.org/changeset/7074
If you upgrade to edge or Rails 2.0 RC1, things will work as you want.
Thanks John. Upgrading isn’t an option right now, but the way I wrote
it, it turns out, should work in either case.
Note that you need the type=“array” attribute in the array entity,
like
so:
Smith
I’m getting the xml by doing a accts = Account.find(:all…), then
accts.to_xml.
So Array.to_xml is not adding that type=“array” part. I guess that’s
also in edge.
-George
On Nov 18, 2007, at 1:50 PM, Josh S. wrote:
Yep, that’s annoying and wrong, but it was fixed months ago in edge:
http://dev.rubyonrails.org/changeset/7074
I see you were the one who filed the ticket. btw, don’t know why I
wrote John instead of Josh earlier, I wasn’t thinking it, it just came
out 