rabl icon indicating copy to clipboard operation
rabl copied to clipboard

Not possible to get parent's array element

Open jonas-jasas opened this issue 11 years ago • 5 comments

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.

jonas-jasas avatar Apr 08 '13 13:04 jonas-jasas

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.

nesquena avatar Apr 08 '13 18:04 nesquena

child :categories do |c|
  # If :categories is an array most of ruby programmers will think that c is an array element
end

jonas-jasas avatar Apr 09 '13 04:04 jonas-jasas

@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?

andrewhubbs avatar Apr 09 '13 20:04 andrewhubbs

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

toxaq avatar Jul 19 '13 00:07 toxaq

+1

andre1810 avatar Jan 28 '15 19:01 andre1810