lilHar
lilHar

Reputation: 1866

Rails array style nested parms

So I have objects that I'm trying to pass to a json-acccepting restful interface using parms and @connection, but the source object and the target are in different formats, so I need to rename them.

So I'm trying to do something like this:

parms = {
  :foo => anBunchOfBars.bar[
    :barID => bar.Identifier
    :bartab => bar.finances.owed
    :barbell => bar.equipment.first
  ]
}

... to generate an outgoing JSON similar to the following:

{
   "foo": [
      {
        "barID": "Irish Pub",
        "bartab": "30 Yen",
        "barbell": "Of the ball."
      }, 
      {
        "barID": "One Gold Bar",
        "bartab": "100 cents",
        "barbell": "fry."
      }
   ]
}

But I can't find the proper syntax. Examples I've found seem to only cycle through a core list and leave out naming items on nested traits (or skip nested traits entirely), and I haven't seen anything that shows how to pull values from a looped item.

What's the proper syntax to format the parms value?

Upvotes: 0

Views: 26

Answers (1)

mrzasa
mrzasa

Reputation: 23327

Assuming that anBunchOfBars is an array of bars:

bars = anBunchOfBars.map do |bar|
  {    
    barID: bar.Identifier
    bartab: bar.finances.owed
    barbell: bar.equipment.first
  }
end

params = { foo: bars }

Upvotes: 2

Related Questions