gpt4 book ai didi

ruby-on-rails - Rails 3.1 与 RABL 的深度嵌套

转载 作者:行者123 更新时间:2023-12-04 06:16:57 26 4
gpt4 key购买 nike

我正在使用 RABL gem 为评论的用户呈现 JSON 用户数据,这些评论是注释的子级,注释的子级是图像的子级。我想做类似的事情:

object @image

node :annotations do
@image.annotations.map { |a| {
:id => a.id,
:x1 => a.x1,
:x2 => a.x2,
:y1 => a.y1,
:y2 => a.y2,
node :comments do
@image.annotations.comments.map { |c| {
:body => c.body,
:user_id => c.user_id,
:name => User.find(c.user_id).name,
:user_icon => user_icon(User.find(c.user_id), 'square', 30)
}}
end
}}
end

我知道这在 RABL 中无效,我也尝试使用 child 而不是 node,但无法以这种方式访问​​用户数据。我应该如何去做这件事,什么是正确的语法来实现这一点?

最佳答案

我从 @calvin-l 得到了这个答案.诀窍是只映射 a.comments 然后以这种方式从每个评论中获取数据:

node :annotations do
@image.annotations.map { |a| {
:id => a.id,
:x1 => a.x1,
:x2 => a.x2,
:y1 => a.y1,
:y2 => a.y2,
:comments => a.comments.map { |c| {
:body => c.body,
:created_at => c.created_at,
:user => {
:id => c.user.id,
:facebook_id => c.user.facebook_id,
:name => c.user.name,
:username => c.user.username
}
}}
}}
end

关于ruby-on-rails - Rails 3.1 与 RABL 的深度嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9428253/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com