# Rails 2.02 dropped JSON attribute tag?

**URL:** <https://www.ruby-forum.com/t/rails-2-02-dropped-json-attribute-tag/124334>\
**Category:** Rails\
**Created:** [December 27, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/rails-2-02-dropped-json-attribute-tag/124334 "2007-12-27T19:06:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [December 27, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/rails-2-02-dropped-json-attribute-tag/124334/1 "2007-12-27T19:06:09Z")

</div>

Hey there

I was scratching my head over why my application breaks when I run on  
a local server but works on my remote server. It turns out that my  
local server is Rails 2.0.2 and my remote server is 1.2.3 and I expect  
the tag “attributes” to be passed with a json stream, before parsing.  
Unfortunately, this was dropped in 2.0.2. Is there any documentation  
on this? Any way to get it back?

Examples:

Rails 1.2.3:

> > Site.find(:first).to\_json  
> > =\> “{attributes: {title: “some bogus title”, location: “some bogus  
> > location”}}”

Rails 2.0.2:

> > Site.find(:first).to\_json  
> > =\> “{title: “some bogus title”, location: “some bogus location”}”

Thanks!  
-Dave

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [January 2, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/rails-2-02-dropped-json-attribute-tag/124334/2 "2008-01-02T15:45:18Z")

</div>

Can anyone verify this? It seems like a bug and I’m wondering how to  
report it?

Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [February 13, 2008, 2:08am UTC](https://www.ruby-forum.com/t/rails-2-02-dropped-json-attribute-tag/124334/3 "2008-02-13T02:08:31Z")

</div>

This looks like it might be related to an issue that I’m having getting  
some code from the book “Google Maps Applications with Rails and Ajax”.  
I am a complete Ruby and Rails beginner, and a fairly rusty coder to  
boot, and so have no idea how the suggestion of helps:

ActiveSupport::JSON.encode(@record.instance\_values)

But appreciate being pointed in the right direction non the less and  
would very much appreciate any further help anyone can give.

Everything is working fine on my dev environment which is running 1.2.6,  
but not on my [Heroku.com](http://Heroku.com) account which is running 2.0.2. The code in  
question I believe is as follows:

def list  
render :text=\>(Marker.find :all).to\_json  
end

and is later called by a Javascript function where the relevant code is  
this:

//tell the request what to do when the state changes.  
request.onreadystatechange = function() {  
//alert(‘Step 1’);  
if (request.readyState == 4) {  
//parse the result to JSON, by eval-ing it.  
//The response is an array of markers  
//alert(‘Step 2’);  
markers=eval(“(” + request.responseText + “)” );  
for (var i = 0; i \< markers.length ; i++) {

```
            var marker=markers[i].attributes
            //alert(Step 3);
            var lat=marker.lat;
            var lng=marker.lng;

```

I get a JavaScript error telling me that ‘marker has no properties’  
which I think kicks in somewhere around the commented out Step 3 alert.

Failing any solution to the above does anyone know how/if it is possible  
to role back to 1.2.6 when already running 2.0.2? This might be best  
whislt I’m using two books which are both written for versions of Rails  
earlier than 2.0.

Any light that anyone can shed on this would be very very much  
appreciated.

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [January 2, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/rails-2-02-dropped-json-attribute-tag/124334/4 "2008-01-02T20:24:33Z")

</div>

I guess this is because prior to Rails 2.0 json encoding was done in a  
generic way for all objects, taking their instance variables and  
constructing a json string from them:  
[http://dev.rubyonrails.org/browser/tags/rel\_1-2-3/activesupport/lib/active\_support/json?rev=7905](http://dev.rubyonrails.org/browser/tags/rel_1-2-3/activesupport/lib/active_support/json?rev=7905)

Rails 2.0 introduced a Json serializer into ActiveRecord that behaves  
differently:  
[http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active\_record/](http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_record/)  
(Check out serialization.rb and serializers/json\_serializer.rb)  
The AR Serializer extracts serializable methods and attributes and  
creates a serializable record out of that which is THEN processed by  
ActiveSupports JsonConverter.

To get the old behaviour, use  
ActiveSupport::JSON.encode(@record.instance\_values)

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [February 14, 2008, 8:38pm UTC](https://www.ruby-forum.com/t/rails-2-02-dropped-json-attribute-tag/124334/5 "2008-02-14T20:38:30Z")

</div>

It’s not a bug, it’s a decision so reporting it will fall on deaf  
ears. If you really need to get it back you can probably do something  
like this:

{:attributes=\>Stie.find(params[:id])}.to\_json

It’d probably also be worth upgrading your dev environment…

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [February 13, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/rails-2-02-dropped-json-attribute-tag/124334/6 "2008-02-13T20:29:49Z")

</div>

For anyone reading my book (Google Maps Applications with Rails and  
Ajax), I posted an explanation along with relevant changes to examples  
in the book here:  
[http://googlemapsbook.com/2008/02/13/to\_json-and-rails-2/](http://googlemapsbook.com/2008/02/13/to_json-and-rails-2/)
