ユーザー、投稿、およびロールモデルを使用して、ruby v. 1.9.3 で実行されている Rails 3 アプリがあります。ユーザーには、役割だけでなく多くの投稿があります。
私の routes.rb ファイルは次のようになります。
namespace :api, defaults: {format: 'json'} do
resources :users do
resources :posts
resources :roles
end
end
すべてのユーザーを一覧表示するときに、関連する投稿と役割を json 応答に含めたいと考えています。次のように、そのうちの1 つだけを含める方法を見つけました。
#users_controller.rb
def index
@users = User.all
respond_with @users, :include => :posts
end
次のjson応答が得られます
[{
"created_at":"2013-06-17T13:10:59Z",
"id":1,
"username":"foo"
"posts":[ a list of all the users posts ]
}]
しかし、私が欲しいのは、次のような結果です。
[{
"created_at":"2013-06-17T13:10:59Z",
"id":1,
"username":"foo"
"posts":[ a list of all the users posts ]
"roles":[ a list of all the users roles ]
}]
これを実現するには、コードをどのように変更すればよいですか?