5

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 では有効でないことはわかっています。また、ノードの代わりに子を使用しようとしましたが、その方法でユーザー データにアクセスできませんでした。これを行うにはどうすればよいですか?これを実現するための適切な構文は何ですか?

4

1 に答える 1

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
于 2012-02-24T18:05:18.030 に答える