I have some xml that looks like this: <accounts> <account> <name>Smith</name> </account> <account> <name>Jones</name> </account> </accounts> 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 <accounts> <account> <name>Smith</name> </account> </accounts> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
George Bailey wrote:> I have some xml that looks like this: > > <accounts> > <account> > <name>Smith</name> > </account> > <account> > <name>Jones</name> > </account> > </accounts> > > 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 > > <accounts> > <account> > <name>Smith</name> > </account> > </accounts> > > 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: <accounts type="array"> <account> <name>Smith</name> </account> </accounts> -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 18, 2007, at 1:50 PM, Josh Susser 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: > > <accounts type="array"> > <account> > <name>Smith</name> > </account> > </accounts>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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 18, 2007, at 1:50 PM, Josh Susser wrote:> Yep, that''s annoying and wrong, but it was fixed months ago in edge: > http://dev.rubyonrails.org/changeset/7074I 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 :-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---