rails/rails — Issue #3508: JSON does not render type attribute for single-table inherited models
Author: bitaxis
Created: 2011-11-04 04:11:25+00:00
State: open

I have the following classes participating in an STI hierarchy:

```ruby
class Server < ActiveRecord::Base
# has database column named "type"
end

class WindowsServer < Server
end

class LinuxServer < Server
end
```

In my ServersController class, I have code snippet that says:

```ruby
def show
@server = Server.find(params[:id])
debugger
respond_to do |format|
format.html # show.html.erb
format.json { render json: @server }
end
end
```

When I examine the ```@server``` object in the debugger, I get the following:

```
(rdb:43) @server.class.name
"WindowsServer"
(rdb:43) @server.type
"WindowsServer"
(rdb:43) @server
#
(rdb:43) @server.to_json
"{\"created_at\":\"2011-11-04T03:38:13Z\",\"description\":\"\",\"environment_id\":1,\"hostname\":\"server1\",\"id\":5,\"ip_address\":\"10.1.2.3\",\"updated_at\":\"2011-11-04T03:38:13Z\"}"
```

It's curious that the ```type``` attribute isn't included in the JSON string. Sure enough, the ```type``` attribute is also not rendered when I fetch this resource in JSON format:

```
$ curl http://localhost:3000/servers/5.json
{"created_at":"2011-11-04T03:38:13Z","description":"","environment_id":1,"hostname":"server1","id":5,"ip_address":"10.1.2.3","updated_at":"2011-11-04T03:38:13Z"}
```

Seems that the ```type``` attribute ought to be included.