我有這個樣本文檔:如何Mongoid迴歸模型的字段中使用Rails和JSON
{
"_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
謝謝!有效! :-) – Ben 2012-04-26 12:56:01