-4

I have an array of new objects of active records I want to save them all in one query and I also want to call before_create callback. I used activerecored-import gem and also tried with insert method these both methods skip the callbacks what is best way to do that?

Asnad Atta
  • 3,855
  • 1
  • 32
  • 49

1 Answers1

1

I would suggest that you read the activerecord-import wiki there are lost of interesting facts on how to use that gem there.

here is a key: you have to require 'activerecord-import'

books = []
10.times do |i| 
  books << Book.new(:name => "book #{i}")
end
Book.import books

this is how you save 10 records with 1 call

Happy coding

MZaragoza
  • 10,108
  • 9
  • 71
  • 116
  • Thanks for your replay but I want to call my callbacks you can read this https://github.com/zdennis/activerecord-import/wiki/Callbacks – Asnad Atta May 13 '15 at 06:31