Last active
November 27, 2019 07:21
-
-
Save KingofHamyang/2561b131302a8c25d8c8847d091dda89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shop.find_each do |shop| | |
botem_weight = shop.botem_weight; | |
actual_weight = shop.wifi_actual_weight; | |
Shop::WeightHistory.find_or_create_by(shop_id: shop.id, weight: botem_weight, weight_type: "botem", date: shop.created_at.strftime("%Y-%m-%d")); | |
Shop::WeightHistory.find_or_create_by(shop_id: shop.id, weight: actual_weight, weight_type: "actual", date: shop.created_at.strftime("%Y-%m-%d")); | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shop.find_each do |shop| | |
shop.measures.where("date >= ?", shop.created_at.strftime("%Y-%m-%d").to_date).each do |measure| | |
botem_weight = measure.wifi_botem_ratio <= 1E-5 ? 0 : 1/measure.wifi_botem_ratio | |
actual_weight = shop.wifi_actual_weight | |
Shop::Weight.find_or_create_by!(shop_id: shop.id, date: measure.date, actual: actual_weight, botem: botem_weight) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rails 4.x 에서 find_each를 쓸 때 해당 모델의 primary key가 Numerical 이 아니면 돌아가지 않는 버그가 있습니다. Measure의 경우 primary key가 date,shop_id이다 보니 작동하지 않습니다. 따라서 where과 each로 했습니다. 이럴경우 메모리에 저 튜플들을 다 올리게 됩니다
1년 기준 -> 365 * shop갯수(약 3000개) -> 약 1,095,000개 -> 튜플하나 1kb 가정 -> 약 1.04GB