rabl icon indicating copy to clipboard operation
rabl copied to clipboard

Is nested map valid in RABL?

Open sabharikarthik opened this issue 4 years ago • 0 comments

To give a brief intro, the relationships between models used here are: 'Classification' has many 'Standards' and 'Standard' has many 'Sections'. I wanted to use a single rabl file for selecting all sections as well as for selecting a single section. Here, @section = nil indicates that we are selecting all sections.

I tried something like this:

collection @classifications, root: 'classifications', object_root: false
 node do |classification|
	partial('feed/shared/addressable_basic_info', :object => classification)
end
 node (:standards) do |classification|
	standard = @section.standard if @section != nil
 	classification.find_standards_by(standard).map do |standard|
 		partial('feed/shared/addressable_basic_info', :object => standard)
 		node(:sections) do
 			standard.find_sections_by(@section).map do |section|
				partial('feed/shared/addressable_basic_info', :object => section)
				node(:audience_type) { 'student'}
 			end
 		end
        end
end

and it gave me an output like:

{"classifications"=>[{"addressable_type"=>"Classification", "addressable_id"=>1, "audience_name"=>"classification name 1", "audience_type"=>"student", "standards"=>[[{"name"=>nil, "options"=>{}, "block"=>"#<Proc:0x0000555e68fd1c60@(eval):18>"}, {"name"=>"audience_type", "options"=>{}, "block"=>"#<Proc:0x0000555e68fd1be8@(eval):21>"}, {"name"=>"standards", "options"=>{}, "block"=>"#<Proc:0x0000555e68fd1b70@(eval):29>"}, {"name"=>"audience_type", "options"=>{}, "block"=>"#<Proc:0x0000555e68ff5a48@(eval):33>"}, {"name"=>"sections", "options"=>{}, "block"=>"#<Proc:0x0000555e68ff59d0@(eval):35>"}, {"name"=>"audience_type", "options"=>{}, "block"=>"#<Proc:0x0000555e69007298@(eval):38>"}, {"name"=>"audience_type", "options"=>{}, "block"=>"#<Proc:0x0000555e69006460@(eval):38>"}]], "sections"=>[[{"name"=>nil, "options"=>{}, "block"=>"#<Proc:0x0000555e68fd1c60@(eval):18>"}, ..........

But this works well with a single map.

When I use 'each' in place of 'map', it prints out the looped object as such and it does not care about node or partials present within.

The output while using 'each' is something like:

{"classifications"=>[{"addressable_type"=>"Classification", "addressable_id"=>1, "audience_name"=>"classification name 1", "audience_type"=>"student", "standards"=>["#<Standard:0x000055a4468e3308>"], "sections"=>["#<Section:0x000055a4468edcb8>", "#<Section:0x000055a4468ed998>"]}]}

I might be completely wrong with the usage of map. Is my approach wrong here? If so, how can I go about this?

sabharikarthik avatar Sep 13 '19 02:09 sabharikarthik