In seed registration, I tried to summarize various things using ruby notation etc.
User.create!(name: 'Tanaka', email: '[email protected]')
User.create!(name: 'Suzuki', email: '[email protected]')
array = [ ['Tanaka', '[email protected]'], ['Suzuki', '[email protected]'] ]
array.each do |first, second|
User.create!(name: first, email: second)
end
[
['Tanaka', '[email protected]'], ['Suzuki', '[email protected]']
].each do |first, second|
User.create!(name: first, email: second)
end
array = %w[Tanaka [email protected]], %w[Suzuki [email protected]]
array.each do |first, second|
User.create!(name: first, email: second)
end
[
%w[Tanaka [email protected] tokyo\I'm from Hachioji],
%w[Suzuki [email protected] Okinawa\I'm from Naha city]
].each do |first, second, third|
User.create!(name: first, email: second, birthplace: third)
end
each_with_index You don't have to write i = 1, i + = 1 or increment processing. Make index easier to handle.
array = [ ['Tanaka', 'Suzuki'] ]
array.each_with_index do |first, i|
User.create!(name: first, index_no: i)
end
each.with_index(n) Specify the start number.
[
['Tanaka', 'Suzuki']
].each.with_index(5) do |first, i|
User.create!(name: first, index_no: i)
end
[
['Tanaka', '[email protected]'], ['Suzuki', '[email protected]']
].each_with_index do |(first, second), i|
User.create!(name: first, email: second, index_no: i)
end
do |first, second|Is no longer necessary.
[
['Tanaka', '[email protected]'], ['Suzuki', '[email protected]']
].each do
User.create!(name: _1, email: _2)
end
Let's run rails db: seed
to reflect the data.
I hope it helps someone.
**% notation ** https://qiita.com/mogulla3/items/46bb876391be07921743 https://www.sejuku.net/blog/46939 each_with_index http://blog59.hatenablog.com/entry/2018/02/28/155417 ** Insert an empty string in% notation ** https://qiita.com/nao58/items/d897c16c9513dcf54ade ** Numbering parameters ** https://qiita.com/jnchito/items/79f0172e60f237e2c542
Recommended Posts