Your approach seems a little off. To properly serialize a Post, you should create a PostSerializer.
If you have already set up a Post model like the following:
class User < AR::Base
has_many :posts
end
class Post < AR::Base
belongs_to :user
end
You can generate a serializer with the commands:
rails g serializer post
rails g serializer user
This will result in the following serializer structure:
class PostSerializer < ActiveModel::Serializer
attributes :id, :title
has_one :user
end
In your controller, ensure that you have set up:
class PostsController
respond_to :html, :json
def show
@post = Post.find(params[:id])
respond_with @post
end
end
After completing these steps, call /posts/1.json to see the serialized data:
{"post":{"id":1,"title":"the title","user":{"id":1,"name":"jesse"}}}