Search code examples
arraysrubypadrino

Please help me access nested array


I have an array that is called from an API. The data returned is like so (tidied up for display purposes).

[
    {
        "MessageThreadID": 30044,
        "CustomerID": 433,
        "MessageType": 1,
        "Subject": "What is my account balance?",
        "OpenDate": "2015-12-21T10:36:00",
        "Closed": false,
        "ClosedDate": null,
        "Messages": [
            {
                "IBMessageID": 30076,
                "MessageThreadID": 30044,
                "MessageText": "What is my account balance?",
                "FromCustomer": true,
                "UserID": null,
                "Date": "2015-12-21T10:36:00"
            },
            {
                "IBMessageID": 30077,
                "MessageThreadID": 30044,
                "MessageText": "£1230.00",
                "FromCustomer": false,
                "UserID": 1,
                "Date": "2015-12-21T10:36:00"
            },
            {
                "IBMessageID": 30078,
                "MessageThreadID": 30044,
                "MessageText": "Thanks you",
                "FromCustomer": true,
                "UserID": null,
                "Date": "2015-12-21T10:37:00"
            }
        ]
    }
]

From this I use the following in my helpers to make the call to the API and return this data.

def contact_messages_threads(customer_id)

  customer_id = @customer.id
  response = get_call('/Messages/GetOpenedMessages/' + customer_id.to_s)
  response = JSON.parse(response.body)

  @openmessagethreads = {}
  @openmessagethreads = response.map do |openmessagethread|
    Contact.new(openmessagethread) 
  end

  return @openmessagethreads

end  

My contact model is as below where the different sections are defined.

class Contact
  attr_accessor :message_customer_ID, :message_type, :message_subject, :message_source, :message_thread_ID, :messages, :messages_test, :message_text, :from_customer, 
  :message_user_id, :message_date, :openmessagethreads
  attr_reader :messages

  def initialize(options)

    @message_customer_ID        = options['CustomerID'].to_s 
    @message_type               = options['MessageType'].to_s 
    @message_subject            = options['Subject'].to_s 
    @message_source             = options['Closed'].to_s 
    @message_thread_ID          = options['MessageThreadID'].to_s 
    @messages                   = options['Messages']
    @messages_test              = options['Messages']['IBMessageID'].to_s
    @message_text               = options['MessageText']
    @from_customer              = options['FromCustomer']
    @message_user_id            = options['UserID']
    @message_date               = options['OpenDate']
    @openmessagethreads = {}

  end

end

Finally in my view I call the data like so:

- contact_messages_threads(@customer.id).each do |openmessagethread|
  = openmessagethread.message_subject

From this I am able to quite easily call data such as IBMessageThreadID, CustomerID etc however I cannot access the Messages array and what is contained within. I have worked at this when i get a spare minute for the last two months and still havent been able to crack it. I think i may need to change my model, api call or possibly view but have been trying different variations I have found online but cant seem to crack it. Any help is greatly appreciated.


Solution

  • To access the array, you need to use array index.

    Instead of using

    @messages_test              = options['Messages']['IBMessageID'].to_s
    

    you need to use, to access to first element the arrayoptions['Messages'] array, below code

    @messages_test              = options['Messages'][0]['IBMessageID'].to_s
    

    You can iterate the array, if you wish, by using

    options['Messages'] each do |item|
        puts item["IBMessageID"]  # for illustration
    end