--My specs --Rails learning history: 4 months --Conditions
Therefore, there may be mistakes, so ** Don't swallow! **
gem'faker
to Gemfilebundle install
on the terminalGemfile
gem 'faker'
Terminal
$ bundle install
--Japaneseize the entire Rails ** OR ** Japaneseize only Faker
config/initializers/locale.rb
Rails.application.config.i18n.default_locale = :ja ##When translating the entire Rail into Japanese
Faker::Config.locale = :ja ##When only Faker is translated into Japanese
##You can translate it into Japanese by setting only one of them.
References Faker's Github Faker usage example (English)
config/locales/faker
config.i18n.load_path
load any directory under config/localesTerminal
##wget [URL of the file you want to download]-P [Location where you want to store downloaded files]
$ wget https://raw.githubusercontent.com/faker-ruby/faker/master/lib/locales/ja.yml -P config/locales/faker
config/application.rb
module Club
class Application < Rails::Application
...
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]
...
end
end
As a prerequisite, the initial data is input to the User model
, Group model
and its two intermediate tables GroupUser model
.
rails db: migrate: reset
and fill the initial data with rails db: seed
.db/seeds.rb
#Initial setting
users_number = 20
groups_number = 20
#test data
User.create!(name: 'Takashi Sato',
email: '[email protected]',
password: 'password',
description: 'Nice to meet you everyone! !! !!')
Group.create!(name: 'swimming',
description: 'We are working hard every day to win the prefecture.')
GroupUser.create!(group_id: 1,
user_id: 1)
#user
users_list = []
users_number.times do
users_list << {name: Faker::Name.unique.name,
email: Faker::Internet.unique.email,
password: 'password',
description: 'nice to meet you!'}
end
User.create!(users_list)
#group
groups_list = []
groups_number.times do
groups_list << {name: Faker::Team.unique.sport,
description: 'Please join us! !!'}
end
Group.create!(groups_list)
# 1/5 chances to join one group
group_users_list = []
User.all.ids.each do |user_id|
Group.all.ids.each do |group_id|
if rand(5) == 0 && (user_id != 1 && group_id != 1)
group_users_list << { user_id: user_id, group_id: group_id}
end
end
end
GroupUser.create!(group_users_list)
Terminal
$ rails db:migrate:reset
$ rails db:seed
users_list
. (I won't use it this time, but it may be useful later)users_number.times
.users_list
in one block process.User.create! (Users_list)
to create all the users stored in users_list
.python
users_list = []
users_number.times do
users_list << {name: Faker::Name.unique.name,
email: Faker::Internet.unique.email,
password: 'password',
description: 'nice to meet you!'}
end
User.create!(users_list)
References Use create and create! properly
It's a hassle to decide who will join which group, so we'll use rand ()
to simplify it.
User.all.ids.each
and Group.all.ids.each
.if rand (5) == 0
with a probability of 1/5. (* Rand (5) outputs numbers from 0 to 4 at random.)(user_id! = 1 && group_id! = 1)
is registered as test data above, so exclude it so that it is not duplicated.GroupUser.create! (Group_users_list)
to create all the data stored in group_users_list
.python
group_users_list = []
User.all.ids.each do |user_id|
Group.all.ids.each do |group_id|
if rand(5) == 0 && (user_id != 1 && group_id != 1)
group_users_list << { user_id: user_id, group_id: group_id}
end
end
end
GroupUser.create!(group_users_list)
References [Introduction to Ruby] Mastering random![Numeric value, character string, array, secure] [Application of Ruby each] Let's comprehensively understand various usages [Rails] I made the initial data of the association
Recommended Posts