2013-11-02 31 views
0

我只是想播種這些座標,但它是創建每個記錄後兩個確切的重複。所以郵政編號1 - 12看起來不錯,但它重複13 - 24.我不明白爲什麼。我相信代碼是好的,但也許我忽略了一些東西。有沒有人看到一個錯誤或更好的方式來做到這一點。播種軌在同一職位數據庫結果被創建兩次

coordinates = { 
    Albany: [42.40, 73.45], 
    Albuquerque: [35.05, 106.39], 
    Amarillo: [35.11, 101.50], 
    Anchorage: [61.13, 149.54], 
    Atlanta: [33.45, 84.23], 
    Austin: [30.16, 97.44], 
    Baker: [44.47, 117.50], 
    Baltimore: [39.18, 76.38], 
    Bangor: [44.48, 68.47], 
    Birmingham: [33.30, 86.50], 
    Bismarck: [46.48, 100.47], 
    Boise: [43.36, 116.13] 
} 

image = #path to image 

coordinates.values.each do |coordinate| 
    Post.create(latitude: coordinate[0], longitude: coordinate[1], image: image) 
end 

而且

[1] pry(main)> Post.all 
    Post Load (1.4ms) SELECT "posts".* FROM "posts" 
=> [#<Post id: 1, latitude: #<BigDecimal:7fc80e901360,'0.424E2',18(45)>, longitude: #<BigDecimal:7fc80e900e10,'0.7345E2',18(45)>, created_at: "2013-11-02 21:19:37", updated_at: "2013-11-02 21:19:37", image: "apple_icon.png">, 
#<Post id: 2, latitude: #<BigDecimal:7fc80e9127f0,'0.3505E2',18(45)>, longitude: #<BigDecimal:7fc80e911c88,'0.10639E3',18(45)>, created_at: "2013-11-02 21:19:37", updated_at: "2013-11-02 21:19:37", image: "apple_icon.png">, 
#<Post id: 3, latitude: #<BigDecimal:7fc80e91bf30,'0.3511E2',18(45)>, longitude: #<BigDecimal:7fc80e91bcd8,'0.1015E3',18(45)>, created_at: "2013-11-02 21:19:37", updated_at: "2013-11-02 21:19:37", image: "apple_icon.png">, 
#<Post id: 4, latitude: #<BigDecimal:7fc80e91a748,'0.6113E2',18(45)>, longitude: #<BigDecimal:7fc80e91a4f0,'0.14954E3',18(45)>, created_at: "2013-11-02 21:19:37", updated_at: " 
#<Post id: 5, latitude: #<BigDecimal:7fc80e919398,'0.3345E2',18(45)>, longitude: #<BigDecimal:7fc80e919140,'0.8423E2',18(45)>, created_at: "2013-11-02 21:19:37", updated_at: "2013-11-02 21:19:37", image: "apple_icon.png">, 
#<Post id: 6, latitude: #<BigDecimal:7fc80e923c30,'0.3016E2',18(45)>, longitude: #<BigDecimal:7fc80e9239d8,'0.9744E2',18(45)>, created_at: "2013-11-02 21:19:37", updated_at: "2013-11-02 21:19:37", image: "apple_icon.png">, 
#<Post id: 7, latitude: #<BigDecimal:7fc80e922420,'0.4447E2',18(45)>, longitude: #<BigDecimal:7fc80e9220d8,'0.1175E3',18(45)>, created_at: "2013-11-02 21:19:37", updated_at: "2013-11-02 21:19:37", image: "apple_icon.png">, 
#<Post id: 8, latitude: #<BigDecimal:7fc80e9207d8,'0.3918E2',18(45)>, longitude: #<BigDecimal:7fc80e920490,'0.7638E2',18(45)>, created_at: "2013-11-02 21:19:37", updated_at: "2013-11-02 21:19:37", image: "apple_icon.png">, 
#<Post id: 9, latitude: #<BigDecimal:7fc80e92aff8,'0.4448E2',18(45)>, longitude: #<BigDecimal:7fc80e92ad78,'0.6847E2',18(45)>, created_at: "2013-11-02 21:19:37", updated_at: "2013-11-02 21:19:37", image: "apple_icon.png">, 
#<Post id: 10, latitude: #<BigDecimal:7fc80e929b80,'0.333E2',18(45)>, longitude: #<BigDecimal:7fc80e929950,'0.865E2',18(45)>, created_at: "2013-11-02 21:19:37", updated_at: "2013-11-02 21:19:37", image: "apple_icon.png">, 
#<Post id: 11, latitude: #<BigDecimal:7fc80e928758,'0.4648E2',18(45)>, longitude: #<BigDecimal:7fc80e9284d8,'0.10047E3',18(45)>, created_at: "2013-11-02 21:19:37", updated_at: "2013-11-02 21:19:37", image: "apple_icon.png">, 
#<Post id: 12, latitude: #<BigDecimal:7fc80e9331f8,'0.4336E2',18(45)>, longitude: #<BigDecimal:7fc80e932f78,'0.11613E3',18(45)>, created_at: "2013-11-02 21:19:37", updated_at: "2013-11-02 21:19:37", image: "apple_icon.png">, 

#### starts repeating at 13 

#<Post id: 13, latitude: #<BigDecimal:7fc80e931d58,'0.424E2',18(45)>, longitude: #<BigDecimal:7fc80e931b28,'0.7345E2',18(45)>, created_at: "2013-11-02 21:19:44", updated_at: "2013-11-02 21:19:44", image: "apple_icon.png">, 
#<Post id: 14, latitude: #<BigDecimal:7fc80e93bda8,'0.3505E2',18(45)>, longitude: #<BigDecimal:7fc80e93b9c0,'0.10639E3',18(45)>, created_at: "2013-11-02 21:19:44", updated_at: "2013-11-02 21:19:44", image: "apple_icon.png">, 
#<Post id: 15, latitude: #<BigDecimal:7fc80e9382c0,'0.3511E2',18(45)>, longitude: #<BigDecimal:7fc80e943c60,'0.1015E3',18(45)>, created_at: "2013-11-02 21:19:44", updated_at: "2013-11-02 21:19:44", image: "apple_icon.png">, 
#<Post id: 16, latitude: #<BigDecimal:7fc80e9429c8,'0.6113E2',18(45)>, longitude: #<BigDecimal:7fc80e9426f8,'0.14954E3',18(45)>, created_at: "2013-11-02 21:19:44", updated_at: "2013-11-02 21:19:44", image: "apple_icon.png">, 
#<Post id: 17, latitude: #<BigDecimal:7fc80e940790,'0.3345E2',18(45)>, longitude: #<BigDecimal:7fc80e940268,'0.8423E2',18(45)>, created_at: "2013-11-02 21:19:44", updated_at: "2013-11-02 21:19:44", image: "apple_icon.png">, 
#<Post id: 18, latitude: #<BigDecimal:7fc80e94a2b8,'0.3016E2',18(45)>, longitude: #<BigDecimal:7fc80e949e30,'0.9744E2',18(45)>, created_at: "2013-11-02 21:19:44", updated_at: "2013-11-02 21:19:44", image: "apple_icon.png">, 
#<Post id: 19, latitude: #<BigDecimal:7fc80e953c00,'0.4447E2',18(45)>, longitude: #<BigDecimal:7fc80e9539d0,'0.1175E3',18(45)>, created_at: "2013-11-02 21:19:44", updated_at: "2013-11-02 21:19:44", image: "apple_icon.png">, 
#<Post id: 20, latitude: #<BigDecimal:7fc80e952238,'0.3918E2',18(45)>, longitude: #<BigDecimal:7fc80e951a90,'0.7638E2',18(45)>, created_at: "2013-11-02 21:19:44", updated_at: "2013-11-02 21:19:44", image: "apple_icon.png">, 
#<Post id: 21, latitude: #<BigDecimal:7fc80e95b6a8,'0.4448E2',18(45)>, longitude: #<BigDecimal:7fc80e95b388,'0.6847E2',18(45)>, created_at: "2013-11-02 21:19:44", updated_at: "2013-11-02 21:19:44", image: "apple_icon.png">, 
#<Post id: 22, latitude: #<BigDecimal:7fc80e95a140,'0.333E2',18(45)>, longitude: #<BigDecimal:7fc80e959ee8,'0.865E2',18(45)>, created_at: "2013-11-02 21:19:44", updated_at: "2013-11-02 21:19:44", image: "apple_icon.png">, 
#<Post id: 23, latitude: #<BigDecimal:7fc80e958cf0,'0.4648E2',18(45)>, longitude: #<BigDecimal:7fc80e958a70,'0.10047E3',18(45)>, created_at: "2013-11-02 21:19:44", updated_at: "2013-11-02 21:19:44", image: "apple_icon.png">, 
#<Post id: 24, latitude: #<BigDecimal:7fc80e963740,'0.4336E2',18(45)>, longitude: #<BigDecimal:7fc80e9634c0,'0.11613E3',18(45)>, created_at: "2013-11-02 21:19:44", updated_at: "2013-11-02 21:19:44", image: "apple_icon.png">] 

任何幫助表示讚賞控制檯。在此先感謝

回答

2

你有沒有做過兩次耙分貝:種子

如果是,兩次創造相同的項目。

當您運行的種子不會刪除記錄,如果你想這樣做把

Post.destroy_all 

上述座標。

難道我回答你的問題嗎?

+0

哇,這很有趣。我通常創建一個rake任務來爲數據庫播種,而不是使用'db/seeds.rb'。我正在使用'Rake db:reset'來刪除像我通常那樣的數據,並沒有意識到它對'seeds.rb'不起作用。謝謝 – fontno

+0

不客氣:) 你應該使用db/seeds.rb文件和rake db:seed來加載。你不需要任何自定義耙子:) – mike90

2

你可以把創建呼叫條件:

coordinates.values.each do |coordinate| 
    if Post.where(latitude: coordinate[0], longitude: coordinate[1]).empty? 
    Post.create(latitude: coordinate[0], longitude: coordinate[1], image: image) 
    end 
end 

這樣你就可以運行耙分貝:種子,並沒有得到重複。

結賬find_or_create_by