0

I'm trying to use jBuilder to construct a custom array which is the needed structure for my javascript function. In this case their is only 1 object in the array. I am aware of the array! method, but that requires the use of a collection. How do I force jbuilder to wrap an array around the object? Structure I'm looking for is shown below:

object = {
  customArray : [{ someKey : 'somevalue' }]
}
Paul
  • 2,021
  • 5
  • 22
  • 33

1 Answers1

2

Not sure if this is what you are asking, but you can do:

x = { :someKey => 'somevalue' }
Jbuilder.encode do |json|
  json.customArray Array.wrap(x)
end

Array#wrap is provided by ActiveSupport and will wrap an item in an array (unless it is an array). It also won't break your Hash like other methods do (e.g., Array(x), x.to_a, [*x]).

Jacob Brown
  • 7,221
  • 4
  • 30
  • 50