Rails 3.1 deep nesting with RABL

I am using a RABL gem to display user JSON data for comment users, which are child annotations that are child elements of images. I would like to do something similar to:

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

I know this is not true in RABL, I also tried using a child instead of node, but I was not able to access the user data in this way. How do I do this and what is the correct syntax for this to happen?

+5
source share
1 answer

I got this answer from @ calvin-l . The trick was to simply display a.comments and then grab the data from each comment this way:

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
+8

All Articles