我用 active_model_serializers;有 3 个类
class UserSerializer < ActiveModel::Serializer
attributes :id, :username, :email, :authentication_token
has_many :posts
has_one :profile
end
class ProfileSerializer < ActiveModel::Serializer
attributes :name, :bio, :avatar
belongs_to :user_id
end
class PostSerializer < ActiveModel::Serializer
attributes :id, :content, :video
belongs_to :user
end
我在 api/v1/posts.json 下可以列出 post 数据和 user 数据,那在移动端就可以 Get 和 initialize 数据,我希望能在 api/v1/posts.json 下也能列出 profile 的数据,这样在移动端的页面可以显示每篇 post 的内容和用户名,用户头像;(这因该是在服务器端把数据都列出来,还是在移动端根据 user_id 查询 profile 数据?)
class Api::V1::PostsController < Api::V1::BaseController
load_and_authorize_resource except: [:index, :show]
def index
posts = Post.all
paginate json: posts, per_page: 2, status: 200
end
[{"id":309,"content":"test","video":{"video":{"url":"http://test.7xo9gv.com1.z0.glb.clouddn.com/uploads/tumblr_nu7jc4y4sI1udfmeqo1_500.gif?e=1465652771\u0026token=VVHcT1wQxm_qS45p-4s2TEM7dtDlWk-yTMrC_eu5:v87uXlAjiQrCKe-kHDaPB7Dxp5="}},"user_id":1,"user":{"id":1,"username":"shin","email":"[email protected]","authentication_token":"S_apCGF3wodNtKzWkeGYtYZ3dJxFPX-8"}}