我有这个示例文档:
{
"_id" : ObjectId("4f98fd1df2699a2f8a000003"),
"comments" : "Foo bar",
"location" : "Somewhere",
"user_id" : ObjectId("4f98fd1df2699a2f8a000001")
}
当我使用控制器检索数据时:
respond_to :json
def index
respond_with Comment.all
end
它返回 JSON:
[{
"_id": "4f98fd1df2699a2f8a000003",
"comments": "Foo bar",
"location": "Somewhere",
"user_id": "4f98fd1df2699a2f8a000001",
}]
我的问题是如何轻松地在响应中包含 User 类的字段?
class User
include Mongoid::Document
field :username
field :first_name
field :last_name
end