rabl
rabl copied to clipboard
Not possible to get parent's array element
Please have a look at http://stackoverflow.com/q/15878381/625521 question. Can't find the way how to get parent's array element. If there is no way this can be a feature request.
Probably easiest way is to drop in and manage the construction using node:
collection @posts
node :categories do |p|
p.categories.map do |c|
partial("categories/show", :object => c).merge(:post_attr => p.post_attr)
end
end
Perhaps not ideal but it works. For cases like this I think a little manual construction isn't a bad thing and you can use partials to avoid duplication.
child :categories do |c|
# If :categories is an array most of ruby programmers will think that c is an array element
end
@jasajona The Rabl docs are pretty explicit about what object gets passed in. Since the symbol you are passing to child
or node
is an arbitrary name for the node, I'm not sure what behavior you think most ruby programmers will be expecting. Care to elaborate on what would be better?
Came looking for this exact case and I agree with @jasajona. I have no idea why inside a collection loop I would want the collection and not the element. You already have access to the collection.
object @user
child :posts do |user|
attribute :title unless user.suspended? #<-- attribute title is refering to a post, not a user... so why pass the user?
#How do we get the post object here?
end
+1