-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?

2
  • Could you please write any code sample here. Commented Apr 22, 2015 at 6:57
  • Have a read of stackoverflow.com/q/2509320/385532 Commented Apr 22, 2015 at 8:57

1 Answer 1

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

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

1 Comment

Thanks for your replay but I want to call my callbacks you can read this github.com/zdennis/activerecord-import/wiki/Callbacks

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.