Skip to content

Instantly share code, notes, and snippets.

@DanCoughlin
Last active February 12, 2021 11:27
Show Gist options
  • Save DanCoughlin/055e1ab0fd0006ca5a3f to your computer and use it in GitHub Desktop.
Save DanCoughlin/055e1ab0fd0006ca5a3f to your computer and use it in GitHub Desktop.
Example of a database seed file for rails
# loop over this 5 times
5.times do |i|
# create a topic with the name provided name and description
t = Topic.create(title: "Topic ##{i}", description: "This topic is cool because everyone loves the topic number ##{i}.")
# create a random number between 1 and 100 and loop that many times create a vote for this topic
rand(1..100).times do |j|
t.votes.create
end
end
# create a user with this email address and password
# you cannot 'mass assign attributes' to users you need to
# split password, password_confirmation onto separate lines
x = User.new
x.email='[email protected]'
# you need to provide both a password and password confirmation
x.password='12345678'
x.password_confirmation='12345678'
x.save!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment