@@ -2,25 +2,42 @@ namespace :db do
2
2
desc "Fill database with sample data"
3
3
task populate : :environment do
4
4
Rake ::Task [ 'db:reset' ] . invoke
5
- admin = User . create! ( name : "Example User" ,
6
-
7
- password : "foobar" ,
8
- password_confirmation : "foobar" )
9
- admin . toggle! ( :admin )
10
- 99 . times do |n |
11
- name = Faker ::Name . name
12
- email = "example-#{ n +1 } @railstutorial.org"
13
- password = "password"
14
- User . create! ( name : name ,
15
- email : email ,
16
- password : password ,
17
- password_confirmation : password )
18
- end
5
+ make_users
6
+ make_microposts
7
+ make_relationships
8
+ end
9
+ end
10
+
11
+ def make_users
12
+ admin = User . create! ( name : "Example User" ,
13
+
14
+ password : "foobar" ,
15
+ password_confirmation : "foobar" )
16
+ admin . toggle! ( :admin )
17
+ 99 . times do |n |
18
+ name = Faker ::Name . name
19
+ email = "example-#{ n +1 } @railstutorial.org"
20
+ password = "password"
21
+ User . create! ( name : name ,
22
+ email : email ,
23
+ password : password ,
24
+ password_confirmation : password )
25
+ end
26
+ end
19
27
20
- users = User . all ( limit : 6 )
21
- 50 . times do
22
- content = Faker :: Lorem . sentence ( 5 )
23
- users . each { | user | user . microposts . create! ( content : content ) }
24
- end
28
+ def make_microposts
29
+ users = User . all ( limit : 6 )
30
+ 50 . times do
31
+ content = Faker :: Lorem . sentence ( 5 )
32
+ users . each { | user | user . microposts . create! ( content : content ) }
25
33
end
26
34
end
35
+
36
+ def make_relationships
37
+ users = User . all
38
+ user = users . first
39
+ followed_users = users [ 2 ..50 ]
40
+ followers = users [ 3 ..40 ]
41
+ followed_users . each { |followed | user . follow! ( followed ) }
42
+ followers . each { |follower | follower . follow! ( user ) }
43
+ end
0 commit comments