3

我知道播种数据库,我运行 rake db:seed 和一切。我已经设置了我的seeds.rb 文件:

users = [{:name => 'User1 Lastname', :email => 'user1@example.com', :password => 'password'},
         {:name => 'User2', :email => 'user2@example.com', :password => 'password'},
         {:name => 'User3', :email => 'user3@example.com', :password => 'password'},
         {:name => 'User4', :email => 'user4@example.com', :password => 'password'},
         {:name => 'User5', :email => 'user5@example.com', :password => 'password'},
         {:name => 'User6', :email => 'user6@example.com', :password => 'password'},
         {:name => 'User7', :email => 'user7@example.com', :password => 'password'},
         {:name => 'User8', :email => 'user8@example.com', :password => 'password'},
         {:name => 'User9', :email => 'user9@example.com', :password => 'password'},
         {:name => 'User10', :email => 'user10@example.com', :password => 'password'},
         {:name => 'User11', :email => 'user11@example.com', :password => 'password'},
         {:name => 'User12', :email => 'user12@example.com', :password => 'password'},
         {:name => 'User13', :email => 'user13@example.com', :password => 'password'},
         {:name => 'User14', :email => 'user14@example.com', :password => 'password'},
         {:name => 'User15', :email => 'user15@example.com', :password => 'password'},
         {:name => 'User16', :email => 'user16@example.com', :password => 'password'},
         {:name => 'User17', :email => 'user17@example.com', :password => 'password'},
         {:name => 'User18', :email => 'user18@example.com', :password => 'password'},
         {:name => 'User19', :email => 'user19@example.com', :password => 'password'},
         {:name => 'User20', :email => 'user20@example.com', :password => 'password'},
     ]

users.each do |user|
  User.create(user)
end

但是现在每当我运行rails server它时,它们都会再次播种!我以为那只应该在我跑步时发生rake db:seed

~$ rails server
=> Booting WEBrick
=> Rails 3.2.14 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Connecting to database specified by database.yml
   (0.1ms)  begin transaction
  User Exists (21.5ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('user1@example.com') LIMIT 1
   (0.1ms)  rollback transaction
   (0.1ms)  begin transaction
  User Exists (1.4ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('user2@example.com') LIMIT 1
   (0.1ms)  rollback transaction
   (0.1ms)  begin transaction
  User Exists (1.3ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('user3@example.com') LIMIT 1
   (0.1ms)  rollback transaction

等 20 个用户的等然后启动它:

[2013-11-07 00:59:14] INFO  WEBrick 1.3.1
[2013-11-07 00:59:14] INFO  ruby 1.9.3 (2013-06-27) [i686-linux]
[2013-11-07 00:59:14] INFO  WEBrick::HTTPServer#start: pid=1742 port=3000

任何帮助将不胜感激!!谢谢

4

2 回答 2

0

原来是我的初始化程序中的东西。另一个队友抄过来测试,忘记拿出来了。谢谢大家!

于 2013-12-04T19:54:28.147 回答
0

我制作种子如下:

users = []
20.times do |n|
  user << {:name => "Oleg#{n} Sobchuk",
           :email => "user#{n}@example.com",
           :password => 'password'}
end

users.each do |user|
  User.find_or_create_by_name_and_email_and_password(user[:name],
                                                     user[:email],
                                                     user[:password])
end

至于我,它是遵循 DRY 和更清晰!

于 2013-12-04T12:55:02.610 回答