# Neither from\_xml nor from\_Json work in rails 2.1

**URL:** <https://www.ruby-forum.com/t/neither-from-xml-nor-from-json-work-in-rails-2-1/154336>\
**Category:** Rails\
**Created:** [November 27, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/neither-from-xml-nor-from-json-work-in-rails-2-1/154336 "2008-11-27T22:39:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [November 27, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/neither-from-xml-nor-from-json-work-in-rails-2-1/154336/1 "2008-11-27T22:39:27Z")

</div>

If you have the need to do a lot of XML processing you are better off  
staying away from rails completely. The State of SOAP on rails is a  
mess to the point of being practically unusable. The XML and json  
deserialization is like playing russian roulette with five bullets in  
the chamber.

Try it yourself.

u = User.find(1).  
xml = u.to\_xml

… Change the xml.

u.from\_xml(xml)

Doesn’t work.

Same with json. Doesn’t work.

Half the time from\_xml can’t parse the xml to\_xml put out.

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [November 27, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/neither-from-xml-nor-from-json-work-in-rails-2-1/154336/2 "2008-11-27T22:48:15Z")

</div>

On Nov 27, 9:38 pm, “Tim U.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> … Change the xml.
> 
> u.from\_xml(xml)
> 
> Doesn’t work.
> 
> Same with json. Doesn’t work.

Minus your mysterious ‘change the xml’ that worked for me.If you could  
produce a more precise example (or at least elaborate on “Doesn’t  
work” (raises an exception, does nothing, has the wrong attributes,  
something else…)  
you might get a more helpful response.

Fred

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [November 27, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/neither-from-xml-nor-from-json-work-in-rails-2-1/154336/3 "2008-11-27T23:11:41Z")

</div>

> Minus your mysterious ‘change the xml’ that worked for me.If you could  
> produce a more precise example (or at least elaborate on “Doesn’t  
> work” (raises an exception, does nothing, has the wrong attributes,  
> something else…)  
> you might get a more helpful response.

Let’s see…

u = User.find(3)

=\> #\<User id: 3, company\_id: 3, login: “tm\_user”, email:  
“removed\_email\_address@domain.invalid”, crypted\_password:  
“b695db100847aa269fb477443a289d875cf2e001”, salt:  
“1e3a4ba1ccb233c2bbd92c3bdbb917307523671b”, timezone: “UTC”,  
remember\_token: nil, remember\_token\_expires\_at: nil, activation\_code:  
“bab0c2bc67d43b6871005559b114b9b62b7b70bb”, activated\_at: “2008-11-19  
22:12:07”, password\_reset\_code: nil, enabled: true, created\_at:  
“2008-11-19 22:12:07”, updated\_at: “2008-11-19 22:12:07”,  
lock\_version: 4, address\_id: nil\>

> > u.to\_xml

=\> “\<?xml version=\"1.0\" encoding=\"UTF-8\"?\>\n\n  
\<activated-at type=“datetime”\>2008-11-19T22:12:07Z\n  
bab0c2bc67d43b6871005559b114b9b62b7b70bb\n  
\<address-id type=“integer” nil=“true”\>\n  
\<company-id type=“integer”\>3\n  
b695db100847aa269fb477443a289d875cf2e001\n  
removed\_email\_address@domain.invalid\n \<enabled  
type=“boolean”\>true\n \<id type=“integer”\>3\n  
\<lock-version type=“integer”\>4\n  
tm\_user\n \<password-reset-code  
nil=“true”\>\n \<remember-token  
nil=“true”\>\n \<remember-token-expires-at  
type=“datetime” nil=“true”\>\n  
1e3a4ba1ccb233c2bbd92c3bdbb917307523671b\n  
UTC\n\n”

Change the XML. Change the timezone from UTC to ABC

a = “\<?xml version=\"1.0\" encoding=\"UTF-8\"?\>\n\n  
\<activated-at type=“datetime”\>2008-11-19T22:12:07Z\n  
bab0c2bc67d43b6871005559b114b9b62b7b70bb\n  
\<address-id type=“integer” nil=“true”\>\n  
\<company-id type=“integer”\>3\n  
b695db100847aa269fb477443a289d875cf2e001\n  
removed\_email\_address@domain.invalid\n \<enabled  
type=“boolean”\>true\n \<id type=“integer”\>3\n  
\<lock-version type=“integer”\>4\n  
tm\_user\n \<password-reset-code  
nil=“true”\>\n \<remember-token  
nil=“true”\>\n \<remember-token-expires-at  
type=“datetime” nil=“true”\>\n  
1e3a4ba1ccb233c2bbd92c3bdbb917307523671b\n  
ABC\n\n”

Apply it back to the record.

> > u.from\_xml(a)

> u.from\_xml(a)

=\> #\<User id: 3, company\_id: 3, login: “tm\_user”, email:  
“removed\_email\_address@domain.invalid”, crypted\_password:  
“b695db100847aa269fb477443a289d875cf2e001”, salt:  
“1e3a4ba1ccb233c2bbd92c3bdbb917307523671b”, timezone: “UTC”,  
remember\_token: nil, remember\_token\_expires\_at: nil, activation\_code:  
“bab0c2bc67d43b6871005559b114b9b62b7b70bb”, activated\_at: “2008-11-19  
22:12:07”, password\_reset\_code: nil, enabled: true, created\_at:  
“2008-11-19 22:12:07”, updated\_at: “2008-11-19 22:12:07”,  
lock\_version: 4, address\_id: nil\>

Mmmmm. timezone is still UTC.

Let’s save it.

> > u.save  
> > =\> true
> 
> > u  
> > =\> #\<User id: 3, company\_id: 3, login: “tm\_user”, email:  
> > “removed\_email\_address@domain.invalid”, crypted\_password:  
> > “b695db100847aa269fb477443a289d875cf2e001”, salt:  
> > “1e3a4ba1ccb233c2bbd92c3bdbb917307523671b”, timezone: “UTC”,  
> > remember\_token: nil, remember\_token\_expires\_at: nil, activation\_code:  
> > “bab0c2bc67d43b6871005559b114b9b62b7b70bb”, activated\_at: “2008-11-19  
> > 22:12:07”, password\_reset\_code: nil, enabled: true, created\_at:  
> > “2008-11-19 22:12:07”, updated\_at: “2008-11-19 22:12:07”,  
> > lock\_version: 5, address\_id: nil\>

Looks like the time zone never got changed.

Let’s try with json

> > u.to\_json  
> > =\> “{“user”: {“salt”:  
> > “1e3a4ba1ccb233c2bbd92c3bdbb917307523671b”, “activated\_at”:  
> > “2008-11-19T22:12:07Z”, “remember\_token\_expires\_at”: null,  
> > “crypted\_password”: “b695db100847aa269fb477443a289d875cf2e001”,  
> > “password\_reset\_code”: null, “lock\_version”: 5,  
> > “activation\_code”: “bab0c2bc67d43b6871005559b114b9b62b7b70bb”,  
> > “timezone”: “UTC”, “id”: 3, “enabled”: true, “address\_id”:  
> > null, “remember\_token”: null, “company\_id”: 3, “login”:  
> > “tm\_user”, “email”: “removed\_email\_address@domain.invalid”}}”
> 
> > j = “{“user”: {“salt”: “1e3a4ba1ccb233c2bbd92c3bdbb917307523671b”, “activated\_at”: “2008-11-19T22:12:07Z”, “remember\_token\_expires\_at”: null, “crypted\_password”: “b695db100847aa269fb477443a289d875cf2e001”, “password\_reset\_code”: null, “lock\_version”: 5, “activation\_code”: “bab0c2bc67d43b6871005559b114b9b62b7b70bb”, “timezone”: “ABC”, “id”: 3, “enabled”: true, “address\_id”: null, “remember\_token”: null, “company\_id”: 3, “login”: “tm\_user”, “email”: “tm\_user@panztelcom”}}”  
> > =\> “{“user”: {“salt”:  
> > “1e3a4ba1ccb233c2bbd92c3bdbb917307523671b”, “activated\_at”:  
> > “2008-11-19T22:12:07Z”, “remember\_token\_expires\_at”: null,  
> > “crypted\_password”: “b695db100847aa269fb477443a289d875cf2e001”,  
> > “password\_reset\_code”: null, “lock\_version”: 5,  
> > “activation\_code”: “bab0c2bc67d43b6871005559b114b9b62b7b70bb”,  
> > “timezone”: “ABC”, “id”: 3, “enabled”: true, “address\_id”:  
> > null, “remember\_token”: null, “company\_id”: 3, “login”:  
> > “tm\_user”, “email”: “tm\_user@panztelcom”}}”
> 
> > j  
> > =\> “{“user”: {“salt”:  
> > “1e3a4ba1ccb233c2bbd92c3bdbb917307523671b”, “activated\_at”:  
> > “2008-11-19T22:12:07Z”, “remember\_token\_expires\_at”: null,  
> > “crypted\_password”: “b695db100847aa269fb477443a289d875cf2e001”,  
> > “password\_reset\_code”: null, “lock\_version”: 5,  
> > “activation\_code”: “bab0c2bc67d43b6871005559b114b9b62b7b70bb”,  
> > “timezone”: “ABC”, “id”: 3, “enabled”: true, “address\_id”:  
> > null, “remember\_token”: null, “company\_id”: 3, “login”:  
> > “tm\_user”, “email”: “tm\_user@panztelcom”}}”
> 
> > u.from\_json(j)  
> > =\> #\<User id: 3, company\_id: 3, login: “tm\_user”, email:  
> > “removed\_email\_address@domain.invalid”, crypted\_password:  
> > “b695db100847aa269fb477443a289d875cf2e001”, salt:  
> > “1e3a4ba1ccb233c2bbd92c3bdbb917307523671b”, timezone: “UTC”,  
> > remember\_token: nil, remember\_token\_expires\_at: nil, activation\_code:  
> > “bab0c2bc67d43b6871005559b114b9b62b7b70bb”, activated\_at: “2008-11-19  
> > 22:12:07”, password\_reset\_code: nil, enabled: true, created\_at:  
> > “2008-11-19 22:12:07”, updated\_at: “2008-11-19 22:12:07”,  
> > lock\_version: 5, address\_id: nil\>
> 
> > u.save  
> > =\> true
> 
> > u  
> > =\> #\<User id: 3, company\_id: 3, login: “tm\_user”, email:  
> > “removed\_email\_address@domain.invalid”, crypted\_password:  
> > “b695db100847aa269fb477443a289d875cf2e001”, salt:  
> > “1e3a4ba1ccb233c2bbd92c3bdbb917307523671b”, timezone: “UTC”,  
> > remember\_token: nil, remember\_token\_expires\_at: nil, activation\_code:  
> > “bab0c2bc67d43b6871005559b114b9b62b7b70bb”, activated\_at: “2008-11-19  
> > 22:12:07”, password\_reset\_code: nil, enabled: true, created\_at:  
> > “2008-11-19 22:12:07”, updated\_at: “2008-11-19 22:12:07”,  
> > lock\_version: 6, address\_id: nil\>
> 
> >

Exact same story.

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [November 27, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/neither-from-xml-nor-from-json-work-in-rails-2-1/154336/4 "2008-11-27T23:22:13Z")

</div>

On Nov 27, 10:10 pm, “Tim U.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Minus your mysterious ‘change the xml’ that worked for me.If you could  
> > produce a more precise example (or at least elaborate on “Doesn’t  
> > work” (raises an exception, does nothing, has the wrong attributes,  
> > something else…)  
> > you might get a more helpful response.
> 
> Let’s see…

Is timezone a protected attribute (as in attr\_protected or not on the  
list given to attr\_accessible) ?  
Is the accessor function timezone= overwridden ? Does it work if you  
change other attributes?

Fred

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [November 27, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/neither-from-xml-nor-from-json-work-in-rails-2-1/154336/5 "2008-11-27T23:35:06Z")

</div>

On Nov 27, 10:26 pm, “Tim U.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Is timezone a protected attribute (as in attr\_protected or not on the  
> > list given to attr\_accessible) ?
> 
> No. It’s changeable by the web GUI too. Also if that was the case  
> you’d expect the validation to throw up an error right?

actually no. if you try to mass assign a attr\_protected attribute then  
it just ignores it (you’ll get a warning in the logs).

> > Is the accessor function timezone= overwridden ? Does it work if you  
> > change other attributes?
> 
> No. It also doesn’t work if you attempt to change other attributies.
> 
> Change the login field to “tm\_user"changed”

weird. I’d check your logs in case there’s a warning in there. This  
sort of thing could happen if you had accidentally overwritten an  
important activerecord method (eg if you had an association called  
attributes) but hard to say without seeing more. you might find out  
more if you were able to reduce this to a minimal example (or work the  
other way - start with a fresh rails app & model and add stuff until  
it breaks)

Fred

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [November 27, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/neither-from-xml-nor-from-json-work-in-rails-2-1/154336/6 "2008-11-27T23:27:29Z")

</div>

> Is timezone a protected attribute (as in attr\_protected or not on the  
> list given to attr\_accessible) ?

No. It’s changeable by the web GUI too. Also if that was the case  
you’d expect the validation to throw up an error right?

> Is the accessor function timezone= overwridden ? Does it work if you  
> change other attributes?

No. It also doesn’t work if you attempt to change other attributies.

Change the login field to “tm\_user"changed”

> > j = “{“user”: {“salt”: “1e3a4ba1ccb233c2bbd92c3bdbb917307523671b”, “activated\_at”: “2008-11-19T22:12:07Z”, “remember\_token\_expires\_at”: null, “crypted\_password”: “b695db100847aa269fb477443a289d875cf2e001”, “password\_reset\_code”: null, “lock\_version”: 5, “activation\_code”: “bab0c2bc67d43b6871005559b114b9b62b7b70bb”, “timezone”: “ABC”, “id”: 3, “enabled”: true, “address\_id”: null, “remember\_token”: null, “company\_id”: 3, “login”: “tm\_user\_changed”, “email”: “tm\_user@panztelcom”}}”

> > u.from\_json(j)  
> > =\> #\<User id: 3, company\_id: 3, login: “tm\_user”, email:  
> > “removed\_email\_address@domain.invalid”, crypted\_password:  
> > “b695db100847aa269fb477443a289d875cf2e001”, salt:  
> > “1e3a4ba1ccb233c2bbd92c3bdbb917307523671b”, timezone: “UTC”,  
> > remember\_token: nil, remember\_token\_expires\_at: nil, activation\_code:  
> > “bab0c2bc67d43b6871005559b114b9b62b7b70bb”, activated\_at: “2008-11-19  
> > 22:12:07”, password\_reset\_code: nil, enabled: true, created\_at:  
> > “2008-11-19 22:12:07”, updated\_at: “2008-11-19 22:12:07”,  
> > lock\_version: 7, address\_id: nil\>
> 
> >

Nope no change. Let’s save it.

> > u.save  
> > =\> true
> 
> > u.login  
> > =\> “tm\_user”
> 
> >

Didn’t change.
