每次运行rails服务器时都会播种数据库

时间:2013-11-07 09:07:54

标签: ruby-on-rails ruby

我知道为数据库播种,我运行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

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

2 个答案:

答案 0 :(得分: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和更清楚的!

答案 1 :(得分:0)

原来我的初始化程序中有些东西。另一位队友将其复制进行测试并忘记将其取出。谢谢大家!