Rails 3.1使用RABL进行深度嵌套

ram*_*z15 5 json ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 rabl

我正在使用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
Run Code Online (Sandbox Code Playgroud)

我知道这在RABL中无效,我也尝试使用child而不是node,但无法以这种方式访问​​用户数据.我应该怎么做这个,并有什么正确的语法来实现这一目标?

ram*_*z15 8

我从@ 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
Run Code Online (Sandbox Code Playgroud)