Pregunta

In the controller I am trying to find contacts via the find_contacts method and set those contacts to an instance variable. I am using a module that queries an API that returns an array of JSON data. I would like to use that JSON data to create new records in the database, and then return all those created records to the controller. My attempt

def find_contacts(params)
  found_contacts = API.query(params[:name], params[:job])
  # found_contacts array of json data [{data}, {data}]

  contacts = found_contacts.reduce([]) do |contacts, contact|
    contacts << Contact.create(contact)
  end
  return contacts
end

Is this a valid way of creating and returning the records?

¿Fue útil?

Solución

You can just use

contacts = Contact.create found_contacts
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top