how to push objects into an array rails 4

1.5k views Asked by At

I am using gem 'activerecord-import' for bulk import in rails application. I have some sample codes as follows:

articles = []

10000.times do |i| 
 articles << Article.new(:name => "Article #{i}") 
end 

Article.import articles

Here in the code,i am importing 10000 new records in couple of seconds.But if i am creating bulk of comments for an article like:

comments = []
@article= Article.find(1)
10000.times do |i| 
  comments << @article.comments.find_or_create_by(:name => "comment #{i}") 
end 

Comment.import comments

While running the above code, comments are get created in the loop without being pushed in the array. How do i push all the comments into the array before creation.Please help me out.

0

There are 0 answers