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' }
                ])

Unicode null

Unicode has a character that represents null.

It is used to represent the end of a string but could also be used when a placeholder character is required. Perhaps as padding, differentiating from ' '.

In Ruby, 0.chr evaluates to the Null character.

There is a can of worms called Null Byte Injection associated with this character.

Object#itself

Object has a method named #itself. It returns the object it’s called on:

> Object.new.itself
=> #<Object:0x00007ffd9dd9df58>

"Hello".itself
=> "Hello"

For example:

> [1,1,2,3,3,3,4,5,5,6].group_by(&:itself)
=> {1=>[1, 1], 2=>[2], 3=>[3, 3, 3], 4=>[4], 5=>[5, 5], 6=>[6]}