Skip to content

Commit

Permalink
Follow up re: idempotency in db/seeds.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
zzak committed Feb 15, 2023
1 parent 0d72275 commit 33abdae
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions guides/source/active_record_migrations.md
Expand Up @@ -1220,13 +1220,18 @@ end

To add initial data after a database is created, Rails has a built-in 'seeds'
feature that speeds up the process. This is especially useful when reloading the
database frequently in development and test environments. To get started with
this feature, fill up `db/seeds.rb` with some Ruby code, and run `bin/rails
db:seed`:
database frequently in development and test environments, or when setting up
initial data for production.

To get started with this feature, open up `db/seeds.rb` and add some Ruby code,
then run `bin/rails db:seed`.

NOTE: The code here should be idempotent so that it can be executed at any point
in every environment.

```ruby
5.times do |i|
Product.create(name: "Product ##{i}", description: "A product.")
["Action", "Comedy", "Drama", "Horror"].each do |genre_name|
MovieGenre.find_or_create_by!(name: genre_name)
end
```

Expand Down

0 comments on commit 33abdae

Please sign in to comment.