Skip to content

Commit 3f21788

Browse files
Rails: Update guidance on development environment seed data. (#721)
Relates to [this proposal][1] for Suspenders. > We have relied on [`dev:prime`][2] for loading data necessary for users to view most of the features of the app in development. > > However, there were a few areas of improvement. > > First, rename `dev` namespace to `development`, and rename [`prime`][1] task to `seed` for improved clarity. > > The, introduces `development:seed:replant` take to create parity with the existing `db:seed:replant` task. We're already doing this in [ChatBot][3]. [1]: thoughtbot/suspenders#1251 [2]: https://thoughtbot.com/blog/priming-the-pump [3]: thoughtbot/chat_bot#52
1 parent 0dc501c commit 3f21788

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

rails/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Guidance on ActiveRecord, ActiveModel, and other model objects.
7979
suffixes.
8080
- Keep `db/schema.rb` or `db/development_structure.sql` under version control.
8181
- Use `db/seeds.rb` for data that is required in all environments.
82-
- Use `dev:prime` rake task for development environment seed data.
82+
- Use `development:db:seed` rake task for development environment seed data. [Example](/rails/how-to/seed-data.md).
8383

8484
## Migrations
8585

rails/how-to/seed-data.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# How to seed development data
2+
3+
```ruby
4+
# lib/development/seeder.rb
5+
6+
require "factory_bot"
7+
8+
module Development
9+
class Seeder
10+
include FactoryBot::Syntax::Methods
11+
12+
def self.load_seeds
13+
new.load_seeds
14+
end
15+
16+
def load_seeds
17+
emails = %w[ralph@example.com ruby@example.com]
18+
19+
emails.each do |email|
20+
create(:user, email:, password: "password") unless User.exists?(email:)
21+
end
22+
end
23+
end
24+
end
25+
```
26+
27+
```rb
28+
# lib/tasks/development.rake
29+
30+
abort "Seeds can only be loaded in local environments" unless Rails.env.local?
31+
32+
namespace :development do
33+
namespace :db do
34+
desc "Loads seed data into development."
35+
task seed: :environment do
36+
Development::Seeder.load_seeds
37+
end
38+
39+
namespace :seed do
40+
desc "Truncate tables of each database for development and loads seed data."
41+
task replant: [ "environment", "db:truncate_all", "development:db:seed" ]
42+
end
43+
end
44+
end
45+
```

0 commit comments

Comments
 (0)