문제

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?

도움이 되었습니까?

해결책

You can just use

contacts = Contact.create found_contacts
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top