mardi 12 juin 2018

Rails 3 Update large amount of data

I want to update a large amount of data, but how I do it now it will take ages to finish. Is there a quicker way?

What I have now is:

class User < ActiveRecord::Base
    has_many :posts
    has_many :articles, through: :posts
end


class Post < ActiveRecord::Base
    belongs_to :user
    has_many :articles
end


class Article < ActiveRecord::Base
    belongs_to :post
end

Now I want to add the User directly to the articles. I added the column user_id to the article model and now I want to add the user_id to all articles. I use the following code:

Article.all.each do |article| 
    article.user_id = article.post.user_id
    article.save!
end

But this takes ages with the 20 million articles I have.

Do you have an idea how I can update the user_id to the Article model quicker?

Aucun commentaire:

Enregistrer un commentaire