A
A
Anton Ipatov2020-05-09 14:31:57
MongoDB
Anton Ipatov, 2020-05-09 14:31:57

How to transfer images from one service to another in Rails?

Hello, how to transfer existing images correctly.
Images are saved in pic as links.
Now I use this code, the problem is that it takes a very long time and not all images are saved.
I use Ruby on Rails, Mongoid, Attachinary and Cloudinary (for image storage)

class User
  include Mongoid::Document

  has_attachment :image, accept: [:jpg, :png, :gif]

  field :pic, type: String

  before_update :migrate_images

  def migrate_images
    self.image_url = self.pic
  end
end


User.where(:pic.exists => true).all.each &:update

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2020-05-09
@2ord

It takes a long time, perhaps because all Users are taken first, and then they are updated one by one. Instead, you should use ActiveRecord update_all and Mongoid-specific: https://www.rubydoc.info/github/mongoid/mongoid/ma... (with Mongoid syntax I may be wrong) Values ​​will be updated in one request...
.all.each &:update
.update_all({ "$set" => { image_url: "$pic" }})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question