你好我正在拼命地渲染包含我所包含的嵌套资源中的方法的json。我尝试了很多变种,但却无法让它运行。
这就是我所拥有的:
format.json {render json: @user, :include => [ :votes, :petitions, :roles ] }
这就是我希望的工作
format.json {render json: @user, :include => {
:votes => { :methods => [ :status, :count_users_voted ] },
:petitions => { :methods => [:status, :count_users_voted] },
:roles
}
}
有人提示吗?
答案 0 :(得分:0)
将这样的内容添加到您的用户模型中:
def as_json(options = { })
super((options || { }).merge({
:methods => [:agrees, :disagrees]
}))
end
def agrees
self.liked_by_count
end
def disagrees
self.disliked_by_count
end
非常直接,我希望这有帮助