2

How to add parent attributes inside its children in rabl template? To do something like this:

  some_root_attr: {
    attr_a: 'rgtr',
    parent: [
      {
        attr_1: 'asd',
        child: {
          attr_3: 6,
          attr_from_parent_array_member: 'cvb'
        }
      },

      {
         attr_1: 'ert',
         child: {
           attr_3: 9,
           attr_from_parent_array_member: 'erty'
         }
      },
      ...
  ]
}

How to get parent array member inside parent array member's child?

Jonas
  • 4,683
  • 4
  • 45
  • 81

2 Answers2

0
object @obj
attributes :attr_a

child :parents do
  attributes :attr_1
  parent = root_object.dup
  child :child do 
    attributes :attr_3
    node(:attr_from_parent_array_member) { parent.attr_from_parent_array_member }
  end
end

NB haven't tested

ted
  • 5,219
  • 7
  • 36
  • 63
  • |parent| is ont an array member, but all array :( – Jonas Apr 08 '13 at 12:33
  • @Jonas, and `node(:attr_from_parent_array_member) { |parent| parent.attr_from_parent_array_member }` also doesn't work? – ted Apr 08 '13 at 13:04
  • Inside child or node block |parent| is :child object and not :parents array member. – Jonas Apr 08 '13 at 13:19
  • "parent = root_object" acts exactly as block parameter. Inside :parents getting array, inside :child getting child object. – Jonas Apr 08 '13 at 14:04
  • @Jonas, yup, but `root_object` inside :parents block is supposed to be a parent instance, not array of parents. This topic might be helpful http://stackoverflow.com/questions/10730223/accessing-the-child-instance-in-a-rabl-template – ted Apr 08 '13 at 14:11
0

nesquena posted solution on github: https://github.com/nesquena/rabl/issues/436

collection @posts

node :categories do |p|
  p.categories.map do |c| 
    partial("categories/show", :object => c).merge(:post_attr => p.post_attr)
  end
end
Jonas
  • 4,683
  • 4
  • 45
  • 81