0

I'm using the gem called omnicontacts that allow me to export my gmail contacts. When I've retrieved my contacts I'd like to save them into a table called contacts.

However I receive the array of emails, then I have to go through each of them in a loop and print out their email. Now I'm wondering how I can save all of them (each email as it's own). Technically I guess I could loop through a create function but that would take up an insane amount of time.

What's the best practice when it comes to receiving an array, then looping through each of them to save them?

Currently I do this in my view:

<h1>Your Contacts</h1>
<div class="list-group">
  <% @contacts.each do |contact| %>
    <div class="list-group-item">
      <div class="media">
        <div class="media-left">
          <%= image_tag avatar_url(contact[:email].to_s), height: '50', width: '50', alt: "#{contact[:name]}", class: 'img-rounded img-object' %>
        </div>
        <div class="media-body">
          <h4 class="list-group-item-heading media-heading"><%= contact[:name].titleize %></h4>
          <p class="list-group-item-text text-muted"><%= contact[:email] %> </p>
        </div>
      </div>
    </div>
  <% end %>
</div>

My controller looks like this:

def create
  @imported_contacts = request.env['omnicontacts.contacts']
  @user = request.env['omnicontacts.user']    
end
3
  • How many contacts do you have? And what would you consider an 'insane' amount of time? If you've got a few thousand contacts or less, the time it takes to loop through and save each one should be pretty manageable. Commented Aug 28, 2015 at 4:30
  • I mean, I guess it's just my curiosity that makes me want to see if it's able to make this 1 call for example instead of 1000 to the db. Commented Aug 28, 2015 at 4:34
  • Also, just to mention, as you already have @contacts array, you don't have to create new contacts, you just have to update their email attribute in the database as shown in my answer below. Commented Aug 28, 2015 at 4:37

2 Answers 2

1

If you loop through your contacts and create them separately, there is lot DB transaction and performance is affected. I would suggest you to use activerecord-import https://github.com/zdennis/activerecord-import. In one create query you should be able insert all your records to DB.

Sign up to request clarification or add additional context in comments.

Comments

0

You actually have to loop through the @contacts and create contact's with email attribute like this:

@contacts.each {|contact| Contact.create(email: contact[:email].to_s)}

Because, each of the contact has their unique email, so you can't create all of them at once, you have to loop through them.

If was some attribute that's common across all the contacts, then you could use update_all to update the attribute for all the contacts at once. But, in this case, contact emails are different for each contact, so you can't update them without looping through them like shown above.

2 Comments

I can't really use update_attribute when I don't have them in my database. I simply get them from Google, and display them.
well I thought you have your contacts in your @contacts array already. If not, then you need to create them with a create call instead of update: Contact.create(:email, contact[:email].to_s). either way, you have to loop through them as the emails are unique for each contact. I have updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.