Code & Clay – Notes to self. Mainly Ruby/Rails.

Rails: How can I create multiple rows at once?

I needed to seed my database with close to 20,000 rows. It took a long time.

The problem was that I was creating the rows one at a time. However, #create can take an array which makes it possible to create multiple rows at once.

Champion.create([
                  { name: 'Viktor', school: 'Durmstrang' },
                  { name: 'Fleur',  school: 'Beauxbatons' }
                ])

If you really want to get to grips with Ruby development and gain a solid understanding of Object Oriented Design, I thoroughly recommend Sandi Metz's Practical Object Oriented Design in Ruby. It's the perfect introduction to OOP and pragmatic Ruby. You can buy it here.

“Meticulously pragmatic and exquisitely articulate, Practical Object Oriented Design in Ruby makes otherwise elusive knowledge available to an audience which desperately needs it. The prescriptions are appropriate both as rules for novices and as guidelines for experienced professionals.”

Katrina Owen, Creator, Exercism

Essential Reading: Learn Rails 6